如何通过 tcl 访问 Menu Tkinter?

How do I access Menu Tkinter via tcl?

下午好!我正在使用 Tkinter GUI 编写 Python 程序。为了实验,我决定重写一个 ttk 主题。整个程序应该是深色,但菜单保持浅色。然后我决定通过tcl文件写Menu的设置,但我只能找到Menubutton的访问权限,我不知道如何访问[=]的设置13=] 本身。请帮助我。

更新

我发现我问错了。我需要在 window

中更改此菜单

这里是

屏幕截图显示菜单是灰色的,但我需要将整个菜单更改为黑色(而不仅仅是在第二个菜单中更改的菜单按钮)

更新 2

我找到了这个 link:https://wiki.tcl-lang.org/page/Changing+Widget+Colors

有这样一段代码:

.menu configure -background color
.menu configure -foreground color
.menu configure -activebackground color
.menu configure -activeforeground color
.menu configure -disabledforeground color
.menu configure -font namedfont
.menu configure -selectcolor color
.menu configure -activeborderwidth size

据我了解,这是一种通过 tcl 文件更改菜单本身的方法,但我不知道如何修改此代码以及如何应用它来调整此菜单栏。

PS 对不起,我的英语很笨拙,但在俄语论坛上没有人帮助我。我不得不采取极端措施

菜单小部件的基本属性是 described here (which hyperlinks to generic option descriptions elsewhere in some cases); they're mapped directly into Tkinter,但那里描述得并不完全。可能给您带来问题的选项可能是以下选项之一:

  • -borderwidth
  • -activeborderwidth
  • -relief(希望不是这个;它由菜单绑定主动管理)

尝试将前两个显式设置为零(无论如何它们通常为零,但可以通过 Xdefaults 从其他地方获取设置)。

yourmenu.config(borderwidth=0, activeborderwidth=0)

后台本身也是用同样的方法配置的(background="black"就可以了)。您可能需要进行一些试验才能正确使用。


如果您要为整个应用程序执行此操作,您可能需要查看通过 Xdefaults 设置这些值。 Tkinter 的书 没有 很好地记录了这一点; Tk 文档是 better 但你仍然需要通过类比来解决问题,因为 API 的这一部分相当晦涩。

tk.option_add("*Menu.borderWidth", "0")
tk.option_add("*Menu.activeBorderWidth", "0")
tk.option_add("*Menu.background", "black")
# etc.

这里的关键是你需要使用option database name(或者option database class name)而不是选项名称(在菜单页面上列出)并在其前面加上*Menu.,因此它适用于将其放入数据库后创建的所有菜单。

发现了一种在 Tkinter 中创建主菜单的相当有趣的方法。您可以创建一个框架并放置菜单按钮,您可以将弹出菜单附加到该框架。

代码:

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

我没有找到别的方法