Tkinter Entry always captures empty string

Asked

Viewed 132 times

0

I am trying to create an application with graphical interface using the Tkinter. The graphical interface consists of a button, one text and of a entry, as in the image below.

Interface

The idea is to type a word in the widget entry Clicking on the button displays the meaning of that word in the widget text.

My code is organized into two classes: one for the GUI and the other for the application itself.

Graphical user interface (to reduce code I removed the part that inserts the image):

 class Gui:
    def __init__(self, master=None):
        if master is None:
            return
        else:
            self.word = StringVar()
            self.word_meaning = None
            self.app_frame = Frame(master)
            self.app_frame.grid()
            self.create_app_frame()
            self.entry_widget = Entry(self.app_frame, textvariable=self.word)
            self.button_widget = Button(self.app_frame, text='Meaning', command=self.__handler_entry)
            self.text_widget = Text(self.app_frame, height=10, width=30)
            self.crete_app_frame()

    def crete_app_frame(self):    
        self.entry_widget.grid(row=0, column=0)
        self.button_widget.grid(row=0, column=1)
        self.text_widget.grid(row=1, column=0, columnspan=2)

    def get_word(self):
        return self.word.get()

    def set_word_meaning(self, meaning):
        self.word_meaning = meaning

    def __handler_entry(self):
        self.text_widget.delete(0., END)
        self.text_widget.insert(END, self.word_meaning)

Application Logic:

class InteractiveDictionary:
    def __init__(self, filename):
        with open(filename, 'r') as file:
            self.data = json.load(file)

    def get_meaning(self, term):
        print('-------------------')
        print(f"world is:{term}")
        print('-------------------')
        term = str(term)
        term = term.lower()
        if term in self.data:
            return self.data[term]
        else:
            return "The world you\'re looking for doesn\'t exist."

Main:

 if __name__ == '__main__':
    window = Tk()
    window.title('Interactive Dictionary')
    dictionary = InteractiveDictionary('words.json')
    app = Gui(master=window)
    word = app.get_word()
    word_meaning = dictionary.get_meaning(word)
    if type(word_meaning) == list:
        for i in word_meaning:
            app.set_word_meaning(i)
    else:
        app.set_word_meaning(word_meaning)
    window.mainloop()

The application itself works correctly when displaying the results on the terminal. However, when I try to do through the GUI the word captured by get_word() is not passed correctly to the dictionary’s get_meaning() method. He passes a str empty!

I suspect the error is related to the way I invoke Tkinter in main. Does anyone have any idea why word be an empty string and not a word captured by the widget Entry??

1 answer

0


The code that is in main has to be in the __handler_entry function().

def __handler_entry(self):
    word = self.get_word()
    word_meaning = self.dictionary.get_meaning(word)
    if type(word_meaning) == list:
        for i in word_meaning:
            self.set_word_meaning(i)
    else:
        self.set_word_meaning(word_meaning)
    self.text_widget.delete(0., END)
    self.text_widget.insert(END, self.word_meaning)
  • If I do it this way I won’t be isolating the application interface. Also you know how to explain why this?

  • I think it makes sense because the code has to run when the button is pressed and not at startup

  • After much analysis I was able to understand

Browser other questions tagged

You are not signed in. Login or sign up in order to post.