简体   繁体   English

为什么会出现错误:“ NameError:未定义全局名称'pupiluserinputbox'”

[英]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.

相关问题 为什么我得到 NameError: global name 'phase' is not defined - Why do I get NameError: global name 'phase' is not defined 为什么会出现错误(NameError名称“ first”未定义 - Why do I get and Error (NameError name 'first' not defined 为什么会出现错误“ NameError:未定义名称'kh'” - Why do I get the error “NameError: name 'kh' is not defined” NameError:未定义全局名称,为什么会收到该错误? - NameError: global name is not defined, why am I getting that error? 为什么将NameError设置为global时会出现NameError:未定义名称“ counter”? - Why do I get a NameError: name 'counter' is not defined, while I set the counter variable to global? 为什么我用 exec() 得到“NameError: name is not defined”? - Why do I get "NameError: name is not defined" with exec()? 为什么我得到 NameError: name 'self' is not defined? - Why do I get NameError: name 'self' is not defined? 为什么我会收到“NameError: name 'df' is not defined”? - Why do I get “NameError: name 'df' is not defined”? 为什么会收到(NameError:未定义全局名称“ secondRoom”)? - Why am I getting (NameError: global name 'secondRoom' is not defined)? 为什么我得到NameError:没有定义全局名称'spacing' - why am i getting NameError: global name 'spacing' is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM