[英]Python 2.7.x Tkinter popup menu throwing tclError
I'm trying to create a popup menu in Tk, but when I get to the part where the menu is to be displayed (menu.post), I get a "TclError" exception. 我正在尝试在Tk中创建一个弹出菜单,但是当我到达要显示菜单的部分时(menu.post),我收到了“ TclError”异常。 I don't understand why, and when I try to step into the Tk code to see what's going wrong, I only get a couple of steps in before I suddenly can't step into code anymore and the exception bubbles up.
我不明白为什么,当我尝试进入Tk代码查看问题所在时,只有几步之遥,然后才突然无法再进入代码,并且异常冒出来。 Can someone tell me what I'm doing wrong?
有人可以告诉我我在做什么错吗? I think it has to do with how I'm creating the menu.
我认为这与我创建菜单的方式有关。
class Bugger(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# setup window attributes
self.overrideredirect(True)
self.attributes('-topmost', 1)
# set starting positions and values
self.TotalAssigned = "0"
self.TotalResolved = "0"
# add first label
self.label1 = tk.Label(self, text=self.TotalAssigned, bg="red")
self.label1.pack(side="left", fill="both", expand=True)
# add second label
self.label2 = tk.Label(self, text=self.TotalResolved, bg="yellow")
self.label2.pack(side="right", fill="both", expand=True)
# add right-click menu
self.menu = tk.Menu(master=self, tearoff=0)
self.menu.add_command(label="Exit", command=self.ExitMenu)
self.menu.add_command(label="Preferences", command=self.Preferences)
self.bind("<ButtonRelease-2>", self.popup)
def ExitMenu(self):
exit(0)
def popup(self, event):
self.menu.post(event.x_root,event.y_root)
def Preferences(self):
print ("In preferences dialog")
if __name__ == "__main__":
bugger = Bugger()
bugger.mainloop()
The TclError exception takes place on the line self.menu.post(event.x_root,event.y_root) TclError异常发生在self.menu.post(event.x_root,event.y_root)行上
Edit: Here's the error I'm getting. 编辑:这是我得到的错误。 I don't know why I didn't include this the first time.
我不知道为什么我第一次不包括这个。
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
return self.func(*args)
File "/Users/robb/source/Bugger.py", line 73, in popup
self.menu.post(event.x_root,event.y_root)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2797, in post
self.tk.call(self._w, 'post', x, y)
TclError TclError
Any advice for me? 对我有什么建议吗?
Ok. 好。 I think that you have to assign the menu to the root window.
我认为您必须将菜单分配给根窗口。 To do this add the line
self.config(menu=self.menu)
. 为此,添加一行
self.config(menu=self.menu)
。 Other wise you are calling the menu without it having a window to display on. 否则,您将在没有菜单显示窗口的情况下调用菜单。
Edit 编辑
Also it can't be set to overrideredirect
because it does not support popup menus. 另外,由于它不支持弹出菜单,因此不能设置为
overrideredirect
。 It also gives an error if you right click on the window when it is not in focus. 如果在窗口不聚焦时右键单击该窗口,也会产生错误。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.