python - Tkinter Entry 总是返回空字符串

标签 python python-3.x oop tkinter

我正在尝试使用 Tkinter 创建 GUI 应用程序。该界面由以下小部件组成:一个按钮、一个条目和一个文本。 下图显示了它。

app image

此应用程序背后的想法是:用户在条目上输入一个单词,然后点击含义按钮,其含义将显示在文本小部件中。

我的代码分为两类:一类用于 GUI,另一类用于应用程序本身。

GUI(删除了之前显示的图像以减少编码):

 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)

应用:

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."

主要:

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

当结果显示在控制台上时,应用程序运行良好。但是,当我尝试在 GUI 上显示时,get_word() 方法捕获的单词未正确传递给词典 get_meaning() 方法。传递了一个

我怀疑这与我在 main 上调用 Tkinter 的方式有关。 我想将 Gui 和应用程序隔离。因此,将 main 中的代码删除到 __handler_entry() 不是一个选项。有人知道如何修复它并使我的应用程序正常运行吗?

最佳答案

看来你不知道 GUI 是如何工作的 - 一切都以 mainloop() 开始,它显示窗口,从系统获取鼠标/键盘事件,将事件发送到小部件,(重新)绘制小部件。

mainloop() 之前的所有内容都会在您看到窗口之前执行 - 因此 word = app.get_word() 在您看到窗口之前执行。您必须在按下按钮时执行的 __handler_entry 中使用它。

更准确地说,在 __handler_event` 中,您将必须使用所有这些

def __handler_entry(self):

    word = self.get_word()

    word_meaning = dictionary.get_meaning(word)

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

关于python - Tkinter Entry 总是返回空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55773536/

相关文章:

python - 如何创建具有可变 alpha 的 3d 条形图

Java 对象为计算变量返回零

python - 为什么 next(iter(x)) 不前进到下一个可迭代对象?

python - 当推文不包含图像时,T​​weepy python 代码在 'media' 实体上返回 KeyError

python / Pandas : Calculation based on cell value

python-3.x - 在keras中打印model.output的张量值输出

Python 3.4 : How to import a module given the full path?

python-3.x - 'utf- 8' codec can' t 解码位置 10 中的字节 0xb5 : invalid start byte

c++ - 未定义对 'class::function' 的引用?

PHP OOP 类设计