我可以在 Tkinter 中更改标题栏吗? [英] Can I change the title bar in Tkinter?

查看:65
本文介绍了我可以在 Tkinter 中更改标题栏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Tkinter 作为我的程序的 GUI,但正如我所见,许多程序没有 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.

例如Tkinter的标题栏:

For example, Tkinter's title bar:

vs GitHub 的标题栏:

vs GitHub's title bar:

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

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

提前致谢!:)

推荐答案

是的,这是可能的.您可以使用 overrideredirect() 在根窗口上杀死标题栏和默认几何设置的方法.之后,您需要从头开始重建所有这些方法,以按照您的需要进行设置.这是一个功能最少的小型工作示例:

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()

这篇关于我可以在 Tkinter 中更改标题栏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆