如何在Mac上的tkinter中使Menu.add_command()工作? [英] How to make Menu.add_command() work in tkinter on the Mac?

查看:500
本文介绍了如何在Mac上的tkinter中使Menu.add_command()工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在OS X上创建了一个tkinter菜单,并尝试使用add_comand()向其添加菜单按钮,则菜单中什么都没有显示.

If I create a tkinter menu on OS X and try to add a menu button to it with add_comand(), nothing shows up in the menu.

如果以下代码在Ubuntu上运行,我将获得一个菜单栏,其中包含两个标记为红色"和蓝色"的命令,它们会更改窗口的背景色.

If the code below is run on Ubuntu, I get a menubar with two commands labeled "Red" and "Blue" that change the background color of the window.

在OS X 10.10.1(Yosemite)上,按钮不出现.我知道我可以使用Red和Blue命令创建一个下拉菜单,但是在我的真实应用程序中,我不想这样做.

On OS X 10.10.1 (Yosemite) the buttons do not appear. I know I can make a dropdown menu with the Red and Blue commands, but in my real app, I'd prefer not to do that.

from platform import python_version_tuple

major = python_version_tuple()[0]

if major == '3':
    import tkinter as tk
else:
    import Tkinter as tk

root = tk.Tk()

fr = tk.Frame(root, height = 200, width = 200)
fr.pack()
menu = tk.Menu(root)
root.configure(menu=menu)
menu.add_command(label='Red', command=lambda:fr.configure(bg='red'))
menu.add_command(label='Blue', command=lambda:fr.configure(bg='blue'))

root.mainloop()

你能告诉我怎么做我想做的事吗?

Can you tell me how to do what I want?

推荐答案

我不认为您可以在OS X上使用本机("Aqua")Tk来做到这一点,您可能不应该尝试. OS X本机菜单无法正常工作,Tk尝试遵循Apple的

I don't think you can do that with the native ("Aqua") Tk on OS X and you probably shouldn't try. OS X native menus don't work like that and Tk tries to follow Apple's Human Interface Guide for menus. You need to have a menu bar with dropdown cascades.

TkDocs网站很好地介绍了 Tk菜单及其平台差异.((您可以在OS X上使用基于X11的Tk,但是不建议这样做,因为Apple不再在OS X上附带X11服务器,并且您的应用在OS X用户中的外观和行为都会很奇怪. )

The TkDocs website has a good introduction to Tk menus and their platform differences. (You could use an X11-based Tk on OS X, but that is not recommended as Apple does not ship X11 servers anymore with OS X and your app would look and behave oddly for OS X users.)

这篇关于如何在Mac上的tkinter中使Menu.add_command()工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆