[英]Tkinter: Change the menu bar and title bar colors
举一个你想要做的事情的例子:
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("400x200")
self.configure(background='black')
self.overrideredirect(1)
self.attributes("-topmost", True)
def startMove(self,event):
self.x = event.x
self.y = event.y
def stopMove(self,event):
self.x = None
self.y = None
def moving(self,event):
x = (event.x_root - self.x)
y = (event.y_root - self.y)
self.geometry("+%s+%s" % (x, y))
def exit(self):
self.destroy()
def save():
print ('save')
return None
def add():
print('add')
return None
class MenuBar(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master, bd=1, relief='raised')
self.master=master
self.configure(background='black',
cursor='hand2')
file = tk.Menubutton(self, text='File',
background='black',
foreground='white',
activeforeground='black',
activebackground='white'
)
file_menu = tk.Menu(file,tearoff=0)
file_menu.add_command(label='save', command=save,
background='black',
foreground='white',
activeforeground='black',
activebackground='white'
)
file.config(menu=file_menu)
file.pack(side='left')
edit = tk.Menubutton(self, text='Edit',
background='black',
foreground='white',
activeforeground='black',
activebackground='white'
)
edit_menu = tk.Menu(edit,tearoff=0)
edit_menu.add_command(label='add', command=add,
background='black',
foreground='white',
activeforeground='black',
activebackground='white'
)
edit.config(menu=edit_menu)
edit.pack(side='left')
close = tk.Button(self, text='X', command=lambda:root.exit(),
background='black',
foreground='white')
close.pack(side='right')
def show():
print('show')
return None
def ex_it():
print('exit')
return None
class MainFrame(tk.LabelFrame):
def __init__(self, master=None):
tk.LabelFrame.__init__(self, master, bd=1, relief='raised', text='MainFrame', background='black', foreground='white')
self.master=master
self.note = tk.Label(self, text='Your typed chars appear here:',
background='black',
foreground='white',
)
self.note.grid(column=0, row=0, columnspan=2, sticky='w')
self.entry = ttk.Entry(self, style='My.TEntry')
self.entry.grid(column=0,row=1,columnspan=3, sticky='ew')
self.columnconfigure(0, weight=1)
self.b_frame=tk.Frame(self, bg='black')
self.b_frame.grid(column=0,row=2,sticky='w')
self.sh_b = tk.Button(self.b_frame, text='Show', command=show)
self.ex_b = tk.Button(self.b_frame, text='Exit', command=ex_it)
self.sh_b.grid(column=0, row=0, sticky='w')
self.ex_b.grid(column=1, row=0, sticky='w', padx=5)
root = App()
menubar = MenuBar(root)
menubar.pack(side='top', fill='x')
mainframe = MainFrame(root)
mainframe.pack(fill='both', expand=1)
menubar.bind("<Button-1>", root.startMove)
menubar.bind("<ButtonRelease-1>", root.stopMove)
menubar.bind("<B1-Motion>", root.moving)
style = ttk.Style(root)
style.element_create("plain.field", "from", "clam")
style.layout("My.TEntry",
[('Entry.plain.field', {'children': [(
'Entry.background', {'children': [(
'Entry.padding', {'children': [(
'Entry.textarea', {'sticky': 'nswe'})],
'sticky': 'nswe'})], 'sticky': 'nswe'})],
'border':'2', 'sticky': 'nswe'})])
style.configure("My.TEntry",
foreground="white",
fieldbackground="grey")
root.mainloop()
玩得开心!
首先,我使用类创建了 3 个对象,它们看起来像这样:
从 Tk() 获取的应用程序/我们的window
class App(tk.Tk):
def __init__(self):
super().__init__()
然后从 Frame 中获取的菜单栏看起来像:
class MenuBar(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master, bd=1, relief='raised')
以及从 tkinter 的 LabeFrame class 中获取的 MainFrame:
class MainFrame(tk.LabelFrame):
def __init__(self, master=None):
tk.LabelFrame.__init__(self, master, bd=1, relief='raised', text='MainFrame', background='black', foreground='white')
要了解有关类和 init 方法的更多信息[单击]了解语法self.
[点击]
让我们仔细看看App:
self.geometry("400x200")
self.configure(background='black')
self.overrideredirect(1)
self.attributes("-topmost", True)
width=400
和height=200
。self.configure(background='black')
设置或获取覆盖重定向标志。 如果非零,这会阻止 window 管理器装饰 window。 也就是说,window 不会有标题和边框,也无法通过普通方式移动或关闭。
(Windows) 如果设置,则此 window 始终放置在其他 windows 之上。 请注意,在此版本中,此属性必须指定为“-topmost”。
使用 overrideredirect 后最大的问题是您不能再移动 window,因为 window 管理器的边框/标题或菜单栏不再存在。 所以我们需要自己携带它,使用以下代码:
def startMove(self,event):
self.x = event.x
self.y = event.y
def stopMove(self,event):
self.x = None
self.y = None
def moving(self,event):
x = (event.x_root - self.x)
y = (event.y_root - self.y)
self.geometry("+%s+%s" % (x, y))
这段代码的作用是通过使用 事件管理器单击/Button-1 来获取当前鼠标位置
event.x 或 event.y 表示:
当前鼠标 position,以像素为单位。
event.x_root 或 event.y_root 表示:
当前鼠标 position 相对于屏幕左上角,以像素为单位。
并且通过从另一个中减去一个我们得到偏移量,我们需要我们的几何方法来“移动” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.