Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the title bar in Tkinter?

Tags:

python

tkinter

I'm using Tkinter as GUI for my program, but as I see, many programs don't have standard look as Tkinter does. By standard look I mean standard title bar, borders, etc.

For example, Tkinter's title bar:

http://pokit.org/get/img/1a343ad92cd8c8f19ce3ca9c27afecba.jpg

vs GitHub's title bar:

http://pokit.org/get/img/cf5cef0eeae5dcdc02f450733fd87508.jpg

See how they have their own custom exit, resize and minimize buttons? Is it possible to achieve that look using Tkinter?

Thanks in advance! :)

like image 609
Amar Kalabić Avatar asked May 23 '14 18:05

Amar Kalabić


People also ask

How do I customize my title bar?

For simple customization, such as changing the title bar color, you can set properties on your app window's title bar object to specify the colors you want to use for title bar elements.

Can I change the title bar color in tkinter?

Tkinter doesn't come with the ability to customize your titlebar built into it. We'll have to get creative and hack around a bit to create a custom title bar. We'll create a titlebar that we can change the color.


2 Answers

Yes it's possible. You can use the overrideredirect() method on the root window to kill the title bar and the default geometry settings. After that, you need to rebuild all those methods from scratch to set it back up like you want. Here's a small working example with minimal functionality:

root = 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 = Frame(root, bg='white', relief='raised', bd=2)

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

# a canvas for the main area of the window
window = 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()
like image 158
atlasologist Avatar answered Sep 22 '22 13:09

atlasologist


I found a way of making the title bar black using ctypes:

Tkinter dark title bar example:

import ctypes as ct


def dark_title_bar(window):
    """
    MORE INFO:
    https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
    """
    window.update()
    DWMWA_USE_IMMERSIVE_DARK_MODE = 20
    set_window_attribute = ct.windll.dwmapi.DwmSetWindowAttribute
    get_parent = ct.windll.user32.GetParent
    hwnd = get_parent(window.winfo_id())
    rendering_policy = DWMWA_USE_IMMERSIVE_DARK_MODE
    value = 2
    value = ct.c_int(value)
    set_window_attribute(hwnd, rendering_policy, ct.byref(value),
                         ct.sizeof(value))

I searched almost a year for a solution!

like image 26
Konstantin Ehmann Avatar answered Sep 18 '22 13:09

Konstantin Ehmann