【发布时间】:2016-04-21 03:34:48
【问题描述】:
所以,我对 Python 还是很陌生,但是一旦将变量放入一个类中,我就无法处理它们。
当没有周围的类时,以下代码可以正常工作,但是一旦添加它就会出现错误:
NameError: name 'someName' is not defined
发生在第 3 行
text = "You have entered " + someName.get()
代码如下:
class GUI:
def changeLabel():
text = "You have entered " + someName.get()
labelText.set(text)
someName.delete(0, END)
someName.insert(0, "You've clicked!")
return
app = Tk()
app.title("GUI Test")
app.geometry('450x300')
labelText = StringVar()
labelText.set("Click when ready")
label1 = Label(app, textvariable=labelText, height=4)
label1.pack()
userInput = StringVar(None)
someName = Entry(app, textvariable=userInput)
someName.pack()
button1 = Button(app, text="Click Here", width=20,command=changeLabel)
button1.pack(side='bottom',padx=15,pady=15)
app.mainloop()
GUI #calling the class to run
任何帮助将不胜感激。
【问题讨论】:
-
GUI #calling the class to run不。这不是你所说的。请先查看python类教程。 -
这里有很多问题。您可能应该重新阅读
TKinter教程。 -
另外,你应该在你的问题中描述你期望你的代码做什么。
标签: python user-interface tkinter python-3.5