我使用Tkinter作为我的程序的GUI,但正如我所见,许多程序没有像Tkinter那样具有标准外观。标准外观我指的是标准标题栏,边框等。
例如,Tkinter的标题栏:
vs GitHub的标题栏:
了解他们如何拥有自己的自定义退出,调整大小和最小化按钮?是否可以使用Tkinter实现这种外观?
提前致谢! :)
答案 0 :(得分:7)
是的,这是可能的。您可以使用根窗口上的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()
答案 1 :(得分:1)
在python3.5.2中,我必须进行一些修改才能使其正常工作:
#custom title bar for tkinter
from tkinter import Tk, Frame, Button, Canvas
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='Close this Window', 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()
答案 2 :(得分:1)
这些是我使用python 3.7.2进行的修改
from Tkinter import *
root=Tk()
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='#2e2e2e', relief='raised', bd=2,highlightthickness=0)
# put a close button on the title bar
close_button = Button(title_bar, text='X', command=root.destroy,bg="#2e2e2e",padx=2,pady=2,activebackground='red',bd=0,font="bold",fg='white',highlightthickness=0)
# a canvas for the main area of the window
window = Canvas(root, bg='#2e2e2e',highlightthickness=0)
# pack the widgets
title_bar.pack(expand=1, fill=X)
close_button.pack(side=RIGHT)
window.pack(expand=1, fill=BOTH)
xwin=None
ywin=None
# bind title bar motion to the move window function
def move_window(event):
root.geometry('+{0}+{1}'.format(event.x_root, event.y_root))
def change_on_hovering(event):
global close_button
close_button['bg']='red'
def return_to_normalstate(event):
global close_button
close_button['bg']='#2e2e2e'
title_bar.bind('<B1-Motion>', move_window)
close_button.bind('<Enter>',change_on_hovering)
close_button.bind('<Leave>',return_to_normalstate)
root.mainloop()
说明:
我们使用bd(border thickness)= 0删除按钮的边框
然后,我们将<Enter>
事件绑定到一个函数 更改前景色。
为了返回其原始状态,我们将<Leave>
事件绑定到另一个函数
注意:该光标不可见,因为我的屏幕捕获软件已将其删除
答案 3 :(得分:1)
enter image description here在这里您更新到了python 3.8 以及标题栏背景和 主要内容背景和标题名称添加 和新背景+清除一些缩进错误
from tkinter import *
root = Tk()
# turns off title bar, geometry
root.overrideredirect(True)
# set new geometry
root.geometry('400x100+200+200')
# set background color of title bar
back_ground = "#2c2c2c"
# set background of window
content_color = "#ffffff"
# make a frame for the title bar
title_bar = Frame(root, bg=back_ground, relief='raised', bd=1, highlightcolor=back_ground,highlightthickness=0)
# put a close button on the title bar
close_button = Button(title_bar, text='x', command=root.destroy,bg=back_ground, padx=5, pady=2, activebackground="red", bd=0, font="bold", fg='white', activeforeground="white", highlightthickness=0)
# window title
title_window = "Title Name"
title_name = Label(title_bar, text=title_window, bg=back_ground, fg="white")
# a canvas for the main area of the window
window = Canvas(root, bg="white", highlightthickness=0)
# pack the widgets
title_bar.pack(expand=1, fill=X)
title_name.pack(side=LEFT)
close_button.pack(side=RIGHT)
window.pack(expand=1, fill=BOTH)
x_axis = None
y_axis = None
# bind title bar motion to the move window function
def move_window(event):
root.geometry('+{0}+{1}'.format(event.x_root, event.y_root))
def change_on_hovering(event):
global close_button
close_button['bg'] = 'red'
def return_to_normal_state(event):
global close_button
close_button['bg'] = back_ground
title_bar.bind('<B1-Motion>', move_window)
close_button.bind('<Enter>', change_on_hovering)
close_button.bind('<Leave>', return_to_normal_state)
root.mainloop()
答案 4 :(得分:0)
大多数人都知道使用&#39; move_window&#39;上面使用的方法;我发现了一个可以获得鼠标精确位置的修复程序,而不是从角落移动:
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)
答案 5 :(得分:0)
好吧,如果有人需要,我留下了旧答案。 它是重新组装Windows 10 bar的自定义任务栏的重新创建
from tkinter import *
window = Tk()
bg = "#f5f6f7"
title_window = "test app"
class app:
def __init__(self, main):
self.main = main
self.main.configure(bg=bg)
self.main.overrideredirect(True)
self.main.geometry('230x130')
self.main.resizable(width=False, height=False)
self.top_bar = Frame(main,bg=bg, cursor="sizing")
self.top_bar.pack(fill=X)
self.title_txt = Label(self.top_bar, text=title_window ,bg=bg)
self.title_txt.pack(side="left", padx=3)
close_btn = Button(self.top_bar,text="x", cursor="arrow", bg=bg, fg="black", highlightthickness=0,activebackground="red", activeforeground="white",bd=0, command=self.main.quit)
close_btn.pack(side="right")
bottom_bar = Frame(main, bg=bg)
bottom_bar.pack()
label_scr = Label(bottom_bar, text="label 1", padx=100, pady=5, bg=bg)
label_scr.grid(row=0, column=0, columnspan=3)
button_scr = Button(bottom_bar, text="Button1", bg=bg, bd=0)
button_scr.grid(row=2, column=0, columnspan=3, pady=3)
button2_scr = Button(bottom_bar, text="Button2", bg=bg,bd=0)
button2_scr.grid(row=3, column=0, columnspan=3, pady=3)
def move_window(event):
window.geometry(f"+{event.x_root}+{event.y_root}")
execution = app(window)
execution.top_bar.bind('<B1-Motion>', move_window)
execution.title_txt.bind('<B1-Motion>', move_window)
window.mainloop()