【问题标题】:NameError: name 'variable' is not defined - tkinterNameError:名称“变量”未定义 - tkinter
【发布时间】:2021-02-23 17:37:49
【问题描述】:

我正在从事一项涉及使用 openpyxl、tkinter 和 pandas 的任务。我需要根据一个输入(avg_trips)在 excel 中进行一些计算

这是当前代码。上传文件后,代码只要求在终端中输入。但我希望它在 tkinter 窗口中要求输入

def open_file():
    file = askopenfile(mode ='a+', filetypes =[('Excel Files', '*.xlsx *.xlsm *.sxc *.ods *.csv *.tsv')]) 
    wb = openpyxl.load_workbook(filename = file.name)
    
    wb.create_sheet("Results")
    avg_trips = float(input("Enter Shipping Frequency: "))

这是我尝试过的 tkinter 代码:

root = tk.Tk()
root.title("Optimization")
root.geometry('500x450')

frq = Label(root,text="Enter Shipping Frequency: ")
frq.pack()
frq.place(x =105,y=180)

def printtext():
    global e
    avg_trips = int(e.get()) 

e = Entry(root)
e.pack()
e.place(x=260,y=180)
button = Button(root,text='okay',command=printtext)
button.pack(side='bottom')
button.place(x=230,y=220)
lbl=Label(root, text="Optimization", fg='blue', width = 450,bg = 'light blue', font=("Helvetica", 16))
lbl.pack(side=TOP, fill=X)
btn = Button(root, text ='Upload File', command = open_file)
btn.place(x=210, y=150)
foot=Label(root, text="2021 Optimization",fg='blue',width = 450,bg = 'light blue',font=("Helvetica", 8))
foot.pack(side=BOTTOM, fill=X)
# avg_trips = Entry(root)
# # avg_trips = x.get()

btn = Button(root, text ='Run', command = excel)
btn.place(x=230, y=250)
root.mainloop()

我给出了相同的变量名 (avg_trips),但出现此错误:

NameError:名称“avg_trips”未定义

如何在 tkinter 文件中询问输入并且按钮触发其余过程?

【问题讨论】:

  • 您在root.title(Optimization") 中缺少"
  • avg_trips是一个局部变量,所以它只在printtext内部可见。
  • printtext 定义的开头添加global avg_trips
  • 还有你为什么注释掉# avg_trips = Entry(root)
  • @CoolCloud,我在顶部创建了另一个条目

标签: python tkinter


【解决方案1】:

改变这个:

def printtext():
    global e
    avg_trips = int(e.get())

到这里:

def printtext():
    # `avg_trips` isn't mutable so it doesn't need the global keyword
    # `e` is mutable so it doesn't need the global keyword
    global avg_trips
    avg_trips = int(e.get())

你也必须改变这个:

def open_file():
    file = askopenfile(mode ='a+', filetypes =[('Excel Files', '*.xlsx *.xlsm *.sxc *.ods *.csv *.tsv')]) 
    wb = openpyxl.load_workbook(filename = file.name)
    
    wb.create_sheet("Results")
    avg_trips = float(input("Enter Shipping Frequency: "))

到这里:

def open_file():
    global avg_trips # Make `avg_trips` a global variable
    file = askopenfile(mode ='a+', filetypes =[('Excel Files', '*.xlsx *.xlsm *.sxc *.ods *.csv *.tsv')]) 
    wb = openpyxl.load_workbook(filename = file.name)
    
    wb.create_sheet("Results")
    avg_trips = float(input("Enter Shipping Frequency: "))

可变对象是可以像列表一样更改的对象。不可变是您无法更改的对象,例如 tuple/str/int。 Python 对用户隐藏了大部分内容,但这有时会使人们感到困惑。基本规则是唯一可变的内置对象是list - 所有其他内置对象都是不可变的。有关可变对象与不可变对象的更多信息,请阅读this

【讨论】:

  • 好的,我不再使用这个错误了。但是我输入的值没有分配给 avg_trips。 '0' 被分配给 avg_trips,无论我输入什么。
  • "avg_trips 不可变,因此不需要全局关键字" ?
  • @Mhcg233366 我编辑了它。您还需要在open_file 中添加global avg_trips
  • @CoolCloud 我编辑了我的答案以包括可变对象与不可变对象之间的区别。
猜你喜欢
  • 2020-04-20
  • 2021-09-16
  • 1970-01-01
  • 2021-09-13
  • 2017-08-30
  • 1970-01-01
  • 2021-09-17
  • 1970-01-01
  • 2018-01-24
相关资源
最近更新 更多