python - 有没有办法在 Mac 上的应用程序窗口内显示菜单栏?

标签 python python-3.x tkinter tk python-3.6

我正在关注 tutorial当完全按照所说的去做时,得到了不同的结果。

我希望菜单栏像教程中一样显示在实际的 tkinter 应用程序窗口中。他正在使用Windows。相反,它显示在计算机的实际顶部菜单栏上。如果这有所作为,我正在使用 Mac。有没有办法来解决这个问题?

这是我的代码:

from tkinter import *


def doNothing():
    label1 = Label(root, text="Doing nothing")
    label1.pack()

root = Tk()

mainmenu = Menu(root)
root.config(menu=mainmenu)

filemenu = Menu(mainmenu)
mainmenu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New Project", command=doNothing)
filemenu.add_command(label="New", command=doNothing)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=doNothing)

editmenu=Menu(mainmenu)
mainmenu.add_cascade(label="Edit", menu=editmenu)
editmenu.add_command(label="Redo", command=doNothing)


root.mainloop()

最佳答案

I am using a Mac if that makes a difference. Is there a way to fix this?


是的,它有所作为。 Tk仍然不支持样式菜单。根据 TkDocs :

You'll notice on some recent Linux distributions that many applications show their menus at the top of the screen when active, rather than in the window itself. Tk does not yet support this style of menus.


更多细节...
在下面它说:

On Mac OS X though, there is a single menubar along the top of the screen, shared by each window. As far as your Tk program is concerned, each window still does have its own menubar; as you switch between windows, Tk will automatically take care of making sure that the correct menubar is displayed at the top of the screen. If you don't specify a menubar for a particular window, Tk will use the menubar associated with the root window; you'll have noticed by now that this is automatically created for you when your Tk application starts.


然后它给了我们一个提示:

Because on Mac OS X all windows have a menubar, it's important to make sure you do define one, either for each window or a fallback menubar for the root window. Otherwise, you'll end up with the "built-in" menubar, which contains menus that are only intended for use when typing commands directly into the interpreter.


此外,this issue says :

The Tk menu command is hooked into the Mac OS X global menubar. If you want a menu without using the global menubar, you will have to write your own top level menu.


here是一个询问如何删除顶部菜单的人,以防您有兴趣在窗口本身上实现您的菜单:

The name that shows up in the menu is derived by OS X from the application name in the executing application bundle. If you don't package your program up as an OS X application bundle, defaults will be used; in the case of Python OS X framework builds, Python provides a Python.app within the framework to allow the Python process to be automatically promoted to a full OS X GUI process.
Probably the simplest approach is to use py2app to create a double-clickable app with the name you want. There's an example in an answer to a similar question on Stackoverflow. And there are some old but still relevant details documented in the Tcl/TkAqua FAQ.


一般来说,根据 this article

On the Macintosh, whenever the toplevel is in front, this menu's cascade items will appear in the menubar across the top of the main monitor. On Windows and Unix, this menu's items will be displayed in a menubar accross the top of the window. These menus will behave according to the interface guidelines of their platforms

...

As noted, menubars may behave differently on different platforms. One example of this concerns the handling of checkbuttons and radiobuttons within the menu. While it is permitted to put these menu elements on menubars, they may not be drawn with indicators on some platforms, due to system restrictions.


其他可能有用的文章
  • How to make Menu.add_command() work in tkinter on the Mac?
  • 关于python - 有没有办法在 Mac 上的应用程序窗口内显示菜单栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42744454/

    相关文章:

    python - 分配字典键和值时出现 KeyError

    python-3.x - 如何在 2to3 转换后修复 print((双括号))?

    Python tkinter TreeView 列大小

    Python Tkinter 如何使文本框滚动

    python - dateutil 和 pytz 给出不同的结果

    python - 输出数组 img 的布局与 cv::Mat 不兼容(step[ndims-1] != elemsize 或 step[1] != elemsize*nchannels)

    python - 关于使用pygame和Python时释放内存

    python - 如何通过Python执行任意shell脚本并传递多个变量?

    python-3.x - 寻找用 C 实现的方法的源代码?

    python - 使用 tkinter 网格