无法在M上的Python tkinter中重新启用菜单

2024-10-02 00:24:48 发布

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

我在Tkinter中创建了一个GUI,它有一个菜单。我想要一个按钮,当点击切换菜单被启用/禁用。在

所以我写了下面这个最小的例子(基于https://mail.python.org/pipermail/tkinter-discuss/2004-September/000204.html),它在我的Windows7和Ubuntu14.04机器上运行良好(使用Python2.7.6和Tkinter版本81008)。然而,当我在mac10.9上尝试相同的代码(同样是在python2.7.6和Tkinter修订版81008下)时,菜单将禁用,但无法重新启用。此外,下面的示例还打印出它认为菜单具有的状态(使用entrycget),并打印出它认为菜单在启用和禁用之间交替的状态。在

有人能重现这种行为吗?这是已知的错误吗?或者有没有其他方法可以在Mac上启用/禁用菜单

from Tkinter import *

root=Tk()

def hello():
    print "hello !"

menubar = Menu(root)
submenu = Menu(menubar, tearoff=0)
submenu.add_command(label="Hello", command=hello)
menubar.add_cascade(label='test', menu=submenu)
root.config(menu=menubar)

def toggle():
    print('I think the menu bar is %s' % menubar.entrycget(0,"state"))
    if menubar.entrycget('test', "state")=="normal":
        print('disabling')
        menubar.entryconfig('test', state=DISABLED)
        print('disbled')
    else:
        print('enabling')
        menubar.entryconfig('test', state=NORMAL)
        print('done')


b = Button(root, text='Toggle', command=toggle)
b.pack()

root.mainloop()

Tags: testhellotkinter状态def菜单rootcommand
1条回答
网友
1楼 · 发布于 2024-10-02 00:24:48

here中所述,这似乎是苹果提供的tk8.5中的一个bug。苹果从OSX10.6开始发布的Tk的Cocoa版本出现了许多问题,其中许多问题在TK8.5的最新版本中得到了修复。使用当前的activetcl8.5.15,您的测试似乎可以正常工作。不幸的是,您无法轻松更改苹果提供的系统Pythons使用的Tcl/Tk版本。一种选择是从python.org网站二进制安装程序和ActiveTcl 8.5.15。这里有更多信息:

https://www.python.org/download/mac/tcltk/

https://www.python.org/downloads/

相关问题 更多 >

    热门问题