为什么菜单按钮在此代码中不起作用?

2024-09-26 22:51:14 发布

您现在位置:Python中文网/ 问答频道 /正文

找到了一种在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()

为什么??请告诉我,有什么问题吗


Tags: importaddtkinterroottkfilemenublack

热门问题