如何在mac上定制tkinter菜单栏?

2024-10-01 11:38:03 发布

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

我正在尝试用tkinter构建一个应用程序。我正在使用Mac OS Big-Sur,我在tkinter{}方面有点困难

enter image description here (当你想看到打开的菜单栏时,你是如何截图的哈哈)

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


Tags: 命令应用程序ostkintermacroottk菜单项
1条回答
网友
1楼 · 发布于 2024-10-01 11:38:03

遗憾的是,我没有足够的声誉发表评论。回答您的子问题:您可以按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()

相关问题 更多 >