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

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

问题描述

如果我在 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) 上,按钮不会出现.我知道我可以使用红色和蓝色命令制作下拉菜单,但在我的真实应用中,我不想这样做.

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

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

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