0

I'm creating a simple class named Dialog. I only want it to return when init method is already finished.

from tkinter import *
class Dialog:
    def __set_data(self):
        self.data = self.entry_.get()
        self.top.destroy()
    def __init__(self, prompt_text = "Prompt:", submit_text = "OK", text_size = 5, button_size = 5,  main_window = Tk()):
        main_window.withdraw()
        top = self.top = Toplevel(main_window)
        Label(top, text=prompt_text).pack()
        self.entry_ = Entry(top)
        self.entry_.pack(padx = text_size)
        button = Button(top, text = submit_text, command = self.__set_data)
        button.pack(pady=button_size)

    def get_data(self):
        return self.data
    data = 0

a = Dialog();
print (a.get_data())

If you run this code, you will get the output 0. I want to the output only show up after the user input. How can I do this?

1 Answers1

1

Firstly, I don't think that you really want to postpone the __init__ method returning. If you do that, your code will never get to tk.mainloop(), and will never actually appear on screen.

Instead, what you want to do is be notified when the data in the Entry widget changes. This is a pretty common thing to do in GUI toolkits; the way tkinter handles it is with Events and Bindings. You can read about them generally here.

One way of performing your particular task (showing the data in self.entry once it has been changed by the user) might be to use the method shown in this question.

Alternatively, you could add a command to your submit button (see here) and read the value of the entry in that method.

Community
  • 1
  • 1
sapi
  • 9,944
  • 8
  • 41
  • 71
  • Sorry, but I'm new to Python and my native language isn't english. Can you add to the question the exactly code I need to add? –  Jun 22 '14 at 21:33
  • I dont know what to do! –  Jun 23 '14 at 18:38
  • @LucasHenrique As a start, you could try only printing the data from your `__set_data` method. For anything more complicated than that, it's well worth having a look at the effbot articles I linked to, as they give a pretty good overview of how events/callbacks in Tk work. – sapi Jun 23 '14 at 21:53