【问题标题】:so i keep getting this name error in my Tkinter code for a school project and i dont quite understand why所以我在一个学校项目的 Tkinter 代码中不断收到这个名称错误,我不太明白为什么
【发布时间】:2020-11-18 02:06:01
【问题描述】:

文件“main.py”,第 47 行,在计算中 如果 (i.get() == 1): NameError: name 'i' is not defined

这就是我不断收到的错误,我不太明白为什么。如果有帮助,我主要是编码初学者。帮助将不胜感激,这里是代码。它是一个货币转换器,但目前不完整,因为我不明白错误是如何解决的


#the name of this app is MyFirstGui
class MyFirstGUI:
  def __init__(self, master):
    self.master = master    
    

    #The title
    self.label_a = Label(master, text="~~~ Currency Converter ~~~")
    self.label_a.pack(padx = 10, pady = 10)  
    

    #Where you would enter the amount of money
    self.label_b = Label(master, text="Enter Amount of Money (CDN)")
    self.label_b.pack()  
    self.money_entry = Entry(master)
    self.money_entry.pack()    

    i = IntVar()
    #These will make bubble buttons appear that you can check
    rad1 = Radiobutton(master,text='US Dollar', value=1, variable=i)
    rad2 = Radiobutton(master,text='British Pound', value=2, variable=i)
    rad3 = Radiobutton(master,text='Chinese Yuan', value=3, variable=i)
    rad4 = Radiobutton(master,text='Euro', value=4, variable=i)
    rad5 = Radiobutton(master,text='South Korean Won', value=5, variable=i)
    rad6 = Radiobutton(master,text='Mexican Peso', value=6, variable=i)
 
    rad1.pack(pady = 5, anchor = 'w')
    rad2.pack(pady = 5, anchor = 'w')
    rad3.pack(pady = 5, anchor = 'w')
    rad4.pack(pady = 5, anchor = 'w')
    rad5.pack(pady = 5, anchor = 'w')
    rad6.pack(pady = 5, anchor = 'w')
    
    self.label_results = Label(master, text="")
    self.label_results.pack(padx = 10, pady = 10)  



    #Button to calculate
    self.button_a = Button(master, text="Calculate!", command=self.calculate)
    self.button_a.pack()   
    
  #Where the calculations take place
  def calculate(self):
     if (i.get() == 1):
      canadian_dollars = float(self.money_entry.get())
      us_dollar = (canadian_dollars * 0.75)
      self.label_results.config(text=("$%.2f Canadian Dollars will be equal to $%.2f when converted to US Dollars." % (canadian_dollars, us_dollar)))
    


root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()```

【问题讨论】:

  • i__init__() 内部的局部变量,因此无法在calculate() 内部访问。将i 更改为self.i(或i 以外的更好的名称)。

标签: python tkinter nameerror


【解决方案1】:

您收到此错误是因为您没有在使用它的函数中(在计算函数中)定义变量 i。

如果您想使用与 init 函数中相同的 i 变量,您可以使用 self.i 而不是仅 i 将 i 设为实例变量,也可以将 i 传入作为计算函数的参数。

【讨论】:

  • 非常感谢您的快速回复!我实际上还没有了解全局变量。如果你不介意解释,我会怎么做?
  • 不要为此应用程序使用全局变量。按照@Ken 的建议,将程序中的i 重命名为self.i
  • 对不起,我不是指全局变量,我指的是实例变量。您可以通过将所有“i”替换为“self.i”来使用实例变量
【解决方案2】:

您在MyFirstGUI 的初始化函数中声明了i,但您并未将其范围扩展到包含其他函数,因此您稍后声明的calculate() 函数无法访问i。如果没有上下文,很难知道该推荐什么,但请尝试:

  1. 如果您打算添加更多可能引用 i 的类,请在脚本顶部、MyFirstGUI 之外声明 iroot = Tk()
  2. 如果i 将仅在第一个 gui 类的上下文中使用,则使用 self.iself.i.get()
  3. 另一种选择是将i 设为全局,但通常强烈建议不要这样做。

如果您不打算添加更多引用i 的类,则重命名为self.i 以便i 继承MyFirstGUI 的范围将是最合适的方法。

如果您希望在单个函数的范围之外使用 i(例如,在 __init__()calculate() 之间使用,那么我也强烈建议您将 i 重命名为更具体的名称。

【讨论】:

  • AttributeError: 'NoneType' object has no attribute '_root' 我通过在顶部声明 i 得到这个错误 编辑: 我让它工作了
  • 抱歉,它应该在root = Tk() 下面,因为IntVar() 是一个需要root 的tkinter 类。尝试将i 的定义移动到root 的声明下方。如有必要,您可能需要将 root 的声明移至脚本顶部。这只有在i 将在MyFirstGUI 之外使用时才合理,即使这样它也违背了良好的OOP 实践——在MyFirstGUI 范围内声明i 并让它继承self 是一个更明智的做法。
  • 很高兴听到!请考虑将此页面上最有用的回复标记为已接受的答案,以帮助遇到此帖子的其他人,如果他们有同样的问题。
猜你喜欢
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
相关资源
最近更新 更多