我可以更改Tkinter中的标题栏吗?

Ama*_*bić 3 python tkinter

我正在使用Tkinter作为我的程序的GUI,但正如我所见,许多程序没有像Tkinter那样的标准外观.标准外观我的意思是标准标题栏,边框等.

例如,Tkinter的标题栏:

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

vs GitHub的标题栏:

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

看看他们如何拥有自己的自定义退出,调整大小和最小化按钮?是否有可能使用Tkinter实现这种外观?

提前致谢!:)

小智 9

大多数人会知道在使用上面使用的“move_window”方法时会出错;我找到了一个可以获取鼠标的确切位置并随之移动而不是从角落移动的修复程序:

    def get_pos(event):
        xwin = app.winfo_x()
        ywin = app.winfo_y()
        startx = event.x_root
        starty = event.y_root

        ywin = ywin - starty
        xwin = xwin - startx


        def move_window(event):
            app.geometry("400x400" + '+{0}+{1}'.format(event.x_root + xwin, event.y_root + ywin))
        startx = event.x_root
        starty = event.y_root


        app.TopFrame.bind('<B1-Motion>', move_window)
    app.TopFrame.bind('<Button-1>', get_pos)
Run Code Online (Sandbox Code Playgroud)


Kon*_*ann 9

我找到了一种使用以下方法使标题栏变黑的方法ctypes:(仅限win11)

Tkinter 深色标题栏示例:

import ctypes as ct


def dark_title_bar(window):
    """
    MORE INFO:
    https://learn.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))
Run Code Online (Sandbox Code Playgroud)

我寻找了近一年的解决方案!


atl*_*ist 8

是的,这是可能的.您可以使用overrideredirect()根窗口上的方法来终止标题栏和默认几何设置.之后,您需要从头开始重建所有这些方法,以便按需要重新设置它们.这是一个功能最少的小工作示例:

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()
Run Code Online (Sandbox Code Playgroud)