[英]Why do I get the error: “NameError: global name 'pupiluserinputbox' is not defined”
def pupiluserpass():
global usernames, passwords
usernameinp = pupiluserinputbox.get()
passwordinp = pupilpasswordinputbox.get()
Why do I get the error: NameError: global name 'pupiluserinputbox'
is not defined? 为什么会出现错误:
NameError: global name 'pupiluserinputbox'
?
The input boxes are in another procedure: 输入框在另一个过程中:
def pupillogin():
ruapupil = Label(app, text = "If you're a pupil log in here:")
ruapupil.grid(row = 1, columnspan = 3, sticky = W)
pupilusername = Label(app, text = "Please enter your Username:")
pupilusername.grid(row = 2, column = 0, sticky = W)
pupiluserinputbox = Entry(app, width = 10)
pupiluserinputbox.grid(row = 2, column = 1, sticky = W)
pupilpassword = Label(app, text = "Please enter your Password:")
pupilpassword.grid(row = 3, column = 0, sticky = W)
pupilpasswordinputbox = Entry(app, width = 10)
pupilpasswordinputbox.grid(row = 3, column = 1, sticky = W)
pupilenter = Button(app, text = "Enter!", command = pupiluserpass)
pupilenter.config(height = 1, width = 8)
pupilenter.grid(row = 4, column = 1, sticky = W)
How can I make this work successfully and not get a NameError
? 我如何才能使这项工作成功完成,而不会出现
NameError
?
For a class
, you should make the variables that carry those errors attributes to self
, ie the current instance of that object. 对于一个
class
,您应该将那些具有错误属性的变量设置为self
,即该对象的当前实例。 This is the de facto way to access these variables in other methods within the class
. 这是在
class
中其他方法中访问这些变量的实际方法。 For example: 例如:
def pupiluserpass():
...
usernameinp = self.pupiluserinbox.get()
passwordinp = self.pupilpasswordinputbox.get()
def pupillogin():
...
self.pupiluserinputbox = Entry(app, width = 10)
self.pupiluserinputbox.grid(row = 2, column = 1, sticky = W)
...
self.pupilpasswordinputbox = Entry(app, width = 10)
self.pupilpasswordinputbox.grid(row = 3, column = 1, sticky = W)
You could also do lots of global
declarations here instead but it is better to use self
. 您也可以在这里做很多
global
声明,但是最好使用self
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.