如何在 mac 上自定义 tkinter 菜单栏?

How to customize tkinter menubar on mac?

我正在尝试使用 tkinter 构建应用程序。我正在使用 Mac OS Big Sur,我在使用 tkinter menus.

时遇到了一些问题

(想看打开的菜单栏怎么截图哈哈)

将菜单项添加到默认 mac 菜单栏没有问题,但我想删除一些无用的。我看到您可以使用此命令自定义“首选项”项。 root.createcommand('tk::mac::ShowPreferences', showMyPreferencesDialog) 但我找不到别的东西。这可能吗?

遗憾的是,我没有足够的声誉来发表评论。回答您的子问题:您可以通过按 Cmd+Shift+3 全屏或按 Cmd+Shift+4 进行矩形选择来截屏。如果这不起作用,您必须检查您的 System Preferences > Keyboard > Shortcuts > Screenshots 设置。

关于你的菜单问题,你可以随时更换整个菜单。这里是a tutorial。请注意,第一个菜单将始终保持不变,因为它不属于应用程序,而是属于系统。

这是教程的副本:

from Tkinter import *

def donothing():
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack()
   
root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_command(label="Save as...", command=donothing)
filemenu.add_command(label="Close", command=donothing)

filemenu.add_separator()

filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=donothing)

editmenu.add_separator()

editmenu.add_command(label="Cut", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Paste", command=donothing)
editmenu.add_command(label="Delete", command=donothing)
editmenu.add_command(label="Select All", command=donothing)

menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)
root.mainloop()