Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:07:20+00:00 2026-06-18T02:07:20+00:00

I’m developing a GUI application that models an essay. Among other things, the user

  • 0

I’m developing a GUI application that models an essay. Among other things, the user can create a new topic and then populate that topic with notes. At the moment, I have two ways of creating new topics: through a dropdown option in the menu (the menu command) and through a button on the main screen (the button command). The button starts life with the text “New Topic”. When the user presses the button, the program makes a new topic, asks the user to name the topic using tkSimpleDialog.askstring, and then sets the button’s text to be the name of the topic and the number of notes in that topic. The button’s command then changes to be adding a note to that topic.

While developing the program, I first verified that the menu command worked. It calls askstring successfully, creating a new popup window that handles input in the way I wanted. However, as soon as I added the button command, the call to askstring failed, even when called via the menu command. The window that should have the askstring dialog is whited out and the program hangs. If I comment out the button command, it works again. If I comment out the menu command, it hangs.

Here’s the code where I add the command to the menu:

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

Here’s the code for 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)

Here’s the code for 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

And, finally, here’s the code for the button command:

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)

Anybody have any idea why binding the lambda expression to the button makes askstring hang?

Edit: Thanks for the comments. Here’s a minimal example that exhibits the behavior:

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

Note that switching from button.bind("<Button-1>", (lambda e, s=self: s.newLabel())) to button = Button(frame, text="askstring", command=(lambda s=self: s.newLabel())) fixes the bug (but doesn’t give me a reference to the button that was pressed). I think the problem has something to do with capturing the event as one of the inputs to the lambda.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:07:21+00:00Added an answer on June 18, 2026 at 2:07 am

    The problem you encountered here is due to the call to wait_window in the dialog you are using (you never call it yourself, but the code that implement the dialog does). For instance, the following code replicates the problem after (likely) two button clicks:

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

    This call to wait_window effectively does what the update command does, and is a typical example of why calling update is a bad thing to do. It enters in conflict with the <Button-1> event being handled, and hangs. The problem is that you will have to live with wait_window being used, since it belongs to the dialog’s code. Apparently, if you bind to <ButtonRelease-1> then this conflict never happens. You could also use the command parameter in the button, which works fine too.

    Lastly, I suggest the following to create the buttons in a cleaner manner based on what you want to achieve:

    for i in range(X):
        btn = Tkinter.Button(text=u'%d' % i)
        btn['command'] = lambda button=btn: some_callback(button)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.