1

I am just trying to make a non-graphic game in Tkinter but am having trouble with the entry widget. How can I wait for "<Return>" to be pressed before printing something?

    from Tkinter import *

    class App:
        def __init__(self, master):

            frame = Frame(master)
            frame.pack()
            f = Frame(master, width=500, height=300)
            f.pack(fill=X, expand=True)

            mt = StringVar()


            menubar = Menu(master)
            menubar.add_command(label="New Game", command=self.new_game)
            menubar.add_command(label="Continue Game", command=self.continue_game)
            master.config(menu=menubar)

            maintext = Label(master, fg="blue", textvariable=mt)
            maintext.pack(side=BOTTOM)
            mt.set("")

            self.e1 = Entry(master)
            self.e1.pack()
            self.e1.bind("<Return>")
            self.e1.lower()


            global mt


        def new_game(self):
            mt.set("What do you want your username to be?")
            self.e1.lift()
            #wait for <Return> to be pressed
                mt.set("Welcome " + self.e1.get())
        def continue_game(self):
            mt.set("Type your username in.")



    root = Tk()

    app = App(root)

    root.mainloop()

I want it to be when I click "New Game" on the top menubar, it shows the entry box and waits for me to type something in, and then click enter. THEN it prints out "Welcome"+(what the person types in). What actually happens is when I click New game, it immediately just prints "Welcome".

By print, I mean aet the Label at the bottom to something else, which is reffered to by "mt.set".

1 Answer 1

0

In GUI programs you don't wait* for something to happen, you respond to events. So, to call a function when the user presses return you would do something like:

    self.e1.bind("<Return>", self._on_return)

The above will call the function _on_return when the user presses the return key.

In your specific code you don't really need a StringVar. For example, you could do this:

def __init__(self, master):
    ...
    self.maintext = Label(master, fg="blue")
    ...

def _on_return(self, event):
    self.maintext.configure(text="Welcome, %s" % self.e1.get())

* strictly speaking, you can wait for things, but that's not the right way to write GUI programs except under specific circumstances, such as waiting for a dialog to be dismissed.

6
  • also, how do i properly lift and lower the widgets? The way shown there doesn't really work, it gives me an error
    – udpatil
    Apr 23, 2012 at 20:44
  • What do you mean by "the way shown". This answer doesn't show how to call lift or lower. There's no trick to it though -- mywidget.lift() is all it takes. Apr 23, 2012 at 20:46
  • I meant the way i tried to do it in my program shown there. is "mywidget" in your comment interchangeble with the widget name?
    – udpatil
    Apr 24, 2012 at 21:14
  • You'll have to show the error, the code looks ok to me. And yes, 'mywidget' is interchangeable with your actual widget name. However, like I said in my answer, you can't wait for the user to input something, that's just not how GUI programming works. Apr 24, 2012 at 22:57
  • where it say self.e1.lower, it doesnt actually hide the entry box, so what should I do to fix that? Also, when I remove the "self", it gives me this error: NameError: global name 'e1' is not defined
    – udpatil
    Apr 26, 2012 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.