当我从lambda调用tkSimpleDialog.askstring时,为什么Tkinter会挂起?

时间:2013-01-30 20:58:01

标签: python lambda tkinter

我正在开发一个模拟论文的GUI应用程序。除此之外,用户可以创建新主题,然后使用注释填充该主题。目前,我有两种创建新主题的方法:通过菜单中的下拉选项(菜单命令)和主屏幕上的按钮(按钮命令)。该按钮以文本“New Topic”开始。当用户按下按钮时,程序会创建一个新主题,要求用户使用tkSimpleDialog.askstring命名主题,然后将按钮的文本设置为主题的名称和该主题中的注释数。然后按钮的命令将更改为为该主题添加注释。

在开发程序时,我首先验证了菜单命令是否有效。它成功调用askstring,创建一个新的弹出窗口,以我想要的方式处理输入。但是,只要我添加了button命令,对askstring的调用就会失败,即使通过菜单命令调用也是如此。应该具有askstring对话框的窗口被取消,程序挂起。如果我注释掉按钮命令,它会再次起作用。如果我注释掉菜单命令,它就会挂起。

这是我将命令添加到菜单的代码:

        TopicBtn.menu.add_command(label="New Topic", underline=0,
                                  command=self.newTopic)

这是newTopic()的代码:

 def newTopic(self, button=None):
     """ Create a new topic. If a Button object is passed, associate that Button
          with the new topic. Otherwise, create a new Button for the topic. """

     topicPrompt = "What would you like to call your new topic?"
     topicName = tkSimpleDialog.askstring("New Topic", topicPrompt)

     if topicName in self.topics.keys():
         print "Error: topic already exists"

     else:
         newTopic = {}
         newTopic["name"] = topicName
         newTopic["notes"] = []
         newTopic["button"] = self.newTopicButton(newTopic, button)

         self.topics[topicName] = newTopic
         self.addToTopicLists(newTopic)

这是newTopicButton()的代码:

 def newTopicButton(self, topic, button=None):
 """ If a Button object is passed, change its text to display the topic name.
      Otherwise, create and grid a new Button with the topic name. """

     if button is None:
         button = Button(self.topicFrame)
         index = len(self.topics)
         button.grid(row=index/self.TOPICS_PER_ROW, column=(index %
             self.TOPICS_PER_ROW), sticky=NSEW, padx=10, pady=10)
     else:
         button.unbind("<Button-1>")

     buttonText = "%s\n0 notes" % topic["name"]
     button.config(text=buttonText)
     button.config(command=(lambda s=self, t=topic: s.addNoteToTopic(t)))

     return button

最后,这是按钮命令的代码:

for col in range(self.TOPICS_PER_ROW):
     button = Button(self.topicFrame, text="New Topic")
     button.bind("<Button-1>", (lambda e, s=self: s.newTopic(e.widget)))
     button.grid(row=0, column=col, sticky=NSEW, padx=10, pady=10)

任何人都知道为什么将lambda表达式绑定到按钮会使askstring挂起?

修改:感谢您的评论。这是展示行为的最小示例:

from Tkinter import *
import tkSimpleDialog

class Min():

    def __init__(self, master=None):
        root = master
        frame = Frame(root)
        frame.pack()

        button = Button(frame, text="askstring")
        button.bind("<Button-1>", (lambda e, s=self: s.newLabel()))
        button.grid()

    def newLabel(self):
        label = tkSimpleDialog.askstring("New Label", "What should the label be?")
        print label

root = Tk()
m = Min(root)
root.mainloop()

请注意,从button.bind("<Button-1>", (lambda e, s=self: s.newLabel()))切换到button = Button(frame, text="askstring", command=(lambda s=self: s.newLabel()))会修复错误(但不会提示我对按下的按钮的引用)。我认为这个问题与将事件捕获为lambda的输入之一有关。

2 个答案:

答案 0 :(得分:1)

您在此遇到的问题是由于您正在使用的对话框中调用wait_window(您自己从不调用它,但实现该对话框的代码确实如此)。例如,以下代码在(可能)两次按下单击后复制问题:

import Tkinter

def test(event=None):
    tl = Tkinter.Toplevel()
    tl.wait_window(tl)

root = Tkinter.Tk()
btn = Tkinter.Button(text=u'hi')
btn.bind('<Button-1>', test)
btn.pack(padx=10, pady=10)
root.mainloop()

wait_window的这一调用有效地完成了update命令的作用,并且是调用update是一件坏事的典型例子。它与正在处理的<Button-1>事件冲突,并挂起。问题是您必须使用wait_window,因为它属于对话框的代码。显然,如果你绑定到<ButtonRelease-1>,那么这种冲突永远不会发生。您也可以使用按钮中的command参数,该参数也可以正常工作。

最后,我建议根据您想要的内容以更干净的方式创建按钮:

for i in range(X):
    btn = Tkinter.Button(text=u'%d' % i)
    btn['command'] = lambda button=btn: some_callback(button)

答案 1 :(得分:0)

我想出了一个解决方法。从最小示例测试开始,问题似乎来自于对绑定进行单独调用,从而接受事件作为lambda的输入。如果有人能够解释为什么会发生这种情况,我会接受他们的回答,但我现在会接受这个。

解决方法不是使用单独的绑定函数,而是创建一个按钮数组,然后将数组中的正确条目作为参数传递给lambda函数(您无法传递按钮本身,因为它正在创建在具有lambda函数的行中。)

以下是代码:

from Tkinter import *
import tkSimpleDialog

class Min():

    def __init__(self, master=None):
        root = master
        frame = Frame(root)
        frame.pack()

        buttons = [None] * 2
        for i in range (2):
            buttons[i] = Button(frame, text="askstring",
                            command=(lambda s=self, var=i: s.newLabel(buttons[var])))
            buttons[i].grid()

    def newLabel(self, button):
        label = tkSimpleDialog.askstring("New Label", "What should the label be?")
        button.config(text=label)
        print label

root = Tk()
m = Min(root)
root.mainloop()