简体   繁体   中英

How to change the title bar in Tkinter

I have made an calculator in python using tkinter but:

Title bar of my Calculator

Title bar of Windows calculator

I want to make it look like the Windows one

Here is how you can make a custom title bar:

import tkinter as tk

root = tk.Tk()

def move_window(event):
    root.geometry('+{0}+{1}'.format(event.x_root, event.y_root))

root.overrideredirect(True) # turns off title bar, geometry
root.geometry('400x100+200+200') # set new geometry

# make a frame for the title bar
title_bar = tk.Frame(root, bg='blue', relief='raised', bd=2) # enter your colour here

# put a close button on the title bar
close_button = tk.Button(title_bar, text='X', command=root.destroy)

# a canvas for the main area of the window
window = tk.Canvas(root, bg='black')

# pack the widgets
title_bar.pack(expand=1, fill="x")
close_button.pack(side="right")
window.pack(expand=1, fill="both")

# bind title bar motion to the move window function
title_bar.bind('<B1-Motion>', move_window)

root.mainloop()

You can change the DWMWAttritbute for that window using ctypes. (Caption_Color in this case)

Here is an example:

import tkinter
from ctypes import windll, byref, sizeof, c_int

root = tkinter.Tk()
root.title("Tkinter Window")
root.configure(bg="#292929")

root.update()

HWND = windll.user32.GetParent(root.winfo_id())

# This attribute is for windows 11
DWMWA_CAPTION_COLOR = 35

COLOR_1 = 0x00292929 # color should be in hex order: 0x00bbggrr

windll.dwmapi.DwmSetWindowAttribute(HWND, DWMWA_CAPTION_COLOR, byref(c_int(COLOR_1)), sizeof(c_int))

root.mainloop()

在此处输入图像描述

This is tested on windows 11, idk about window 10, maybe have to change DWMWA_CAPTION_COLOR = 34

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM