找到了一种在Tkinter GUI
-Menubutton
中创建菜单的非常有趣的方法。但不幸的是,这段代码不起作用(或者更确切地说,当您单击Menubutton
时,绑定菜单不会打开):
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.option_add("*Menu.borderWidth", "0")
root.option_add("*Menu.activeBorderWidth", "0")
root.option_add("*Menu.background", "black")
style = ttk.Style(root)
menu = tk.Menu(root)
btn_menu = ttk.Menubutton(root, text='fegvd')
btn_menu.pack()
file = tk.Menu(btn_menu, tearoff=0, foreground='white')
file.add_command(label='ГЫГ')
style.configure('TMenubutton', background='black', foreground='white', indicatoron=0, menu=file, direction='delow', state='active')
root.mainloop()
尽管如此,如果我使用的不是ttk.Menubutton
,而是tk.Menubutton
,那么一切都可以:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.option_add("*Menu.borderWidth", "0")
root.option_add("*Menu.activeBorderWidth", "0")
root.option_add("*Menu.background", "black")
menu = tk.Menu(root)
btn_menu = tk.Menubutton(root, text='fegvd')
btn_menu.pack()
file = tk.Menu(btn_menu, tearoff=0, foreground='white')
file.add_command(label='ГЫГ')
btn_menu.configure(background='black', foreground='white', indicator=0, menu=file, state='active')
root.mainloop()
为什么??请告诉我,有什么问题吗
不能使用样式将菜单与菜单按钮关联。您需要像使用
tk
菜单一样执行此操作:相关问题 更多 >
编程相关推荐