为什么它说“条目”没有定义?

史丹利

每次我运行这个,输入一些东西,然后点击按钮,它说“名称'条目'未定义”。为什么?我认为“条目”已定义。

def displayText():
    textToDisplay=entry.get()
    label.config(text=textToDisplay)

def main():
    import tkinter as tk

    window=tk.Tk()

    label=tk.Label(master=window, text="When you press the button below, whatever is in the text box will be displayed here")
    label.pack()

    entry=tk.Entry(width=10, bg="white", fg="black")
    entry.pack()

    button=tk.Button(text="Click", width=10, command=displayText)
    button.pack()

    entry.insert(0, "")

    window.mainloop()
    sys.exit(0)

if(__name__=="__main__"):
    main()
Tkinter 情人

这是因为变量entry是在一个单独的函数中定义的,然后是你调用它的地方。有两种方法可以解决这个问题。

a) 使用全局变量

def displayText():
    textToDisplay=entry.get()
    label.config(text=textToDisplay)


def main():
    import tkinter as tk
    global entry, label

    window=tk.Tk()

    label=tk.Label(master=window, text="When you press the button below, whatever is in the text box will be displayed here")
    label.pack()

    entry=tk.Entry(width=10, bg="white", fg="black")
    entry.pack()

    button=tk.Button(text="Click", width=10, command=displayText)
    button.pack()

    entry.insert(0, "")

    window.mainloop()
    sys.exit(0)

if(__name__=="__main__"):
    main()

b) 使用类


class GUI:
    def __init__(self):
        self.main()

    def displayText(self):
        textToDisplay=self.entry.get()
        self.label.config(text=textToDisplay)

    def main(self):
        import tkinter as tk

        window=tk.Tk()

        self.label=tk.Label(master=window, text="When you press the button below, whatever is in the text box will be displayed here")
        self.label.pack()

        self.entry=tk.Entry(width=10, bg="white", fg="black")
        self.entry.pack()

        button=tk.Button(text="Click", width=10, command=self.displayText)
        button.pack()

        self.entry.insert(0, "")

        window.mainloop()
        sys.exit(0)

if(__name__=="__main__"):
    GUI()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么它说我的方法没有在 javascript 中定义?

为什么Pylint说numpy.random因为没有“普通”成员,所以它没有?

为什么说即使我已经定义了您也没有定义

为什么我的 Discord bot 代码说有些东西没有定义?

为什么我的C ++编译器说虚拟成员没有定义?

为什么我不能将类指针传递给访问者类?它说没有匹配的功能

Flutter 说 fontFamily 是什么时候没有定义?

为什么说这个列表没有填充?

为什么说我的谓词block / 3没有被调用?

为什么DBMS说表中没有主键

为什么DéjàDup说没有剩余空间了?

为什么说没有选择数据库?

为什么 VSCode 说 Python 没有安装?

一个函数混淆,没有做什么它说,它

为什么说readlink说没有这样的文件或目录

当我没有在原始类型定义中定义类型类约束时,为什么要添加它?

为什么我的WeakHashMap条目没有被GC删除?

为什么END键没有terminfo条目?

NLog:为什么 BufferingWrapper 似乎没有缓冲日志条目

为什么(>>)没有定义为(*>)?

forEach 没有定义——为什么?

为什么没有定义 ID (ejs)?

为什么没有定义“f”?

为什么setVisibility()函数没有定义?

人们为什么说python慢,因为它被解释了?它具有.pyc文件

为什么当我只有一个公共类和类文件时说我的公共类没有定义

为什么它的显示类没有发现错误?

为什么它没有突出显示正确的数字?

为什么地图功能没有完成它的执行