我正在尝试使用tkinter
和来自csv的数据运行新冠病毒19分析代码。但我得到了这个错误,即使我使用了全局也比错误再次出现
from tkinter import *
import tkinter as tk
import pandas as pd
data = pd.read_csv('STATE_WISE_COVID_DATA.csv')
window = tk.Tk()
window.geometry("500x500")
window.title("Pandemic Analytics Engine")
window.configure(background='Light Grey')
tk.Label(window,text = "Pandemic Analyzer", bg='Light Grey' ,fg = 'Dark Blue',font = "none 14 bold").grid(row = 0,column = 0,sticky= 'W')
tk.Label(window,text = "Select State", bg='Light Grey' ,fg = 'White',font = "none 14 bold").grid(row = 2,column = 0,sticky= 'W')
variable = StringVar(window)
variable.set(data.State[0])
w = OptionMenu(window, variable, *data.State)
w.grid(row=2,column=6,sticky='W')
def click():
global inf
global rec
global dec
inf = tk.Label(window,text = int(data[data.State == variable.get()].Confirmed), bg='Orange' ,fg = 'Black',font = "none 14 bold").grid(row = 4,column = 6,sticky= 'W')
rec = tk.Label(window,text = int(data[data.State == variable.get()].Recovered), bg='Orange' ,fg = 'Black',font = "none 14 bold").grid(row = 6,column = 6,sticky= 'W')
dec = tk.Label(window,text = int(data[data.State == variable.get()].Deceased), bg='Orange' ,fg = 'Black',font = "none 14 bold").grid(row = 8,column = 6,sticky= 'W')
Button(window,text = 'OK',width = 6,command = click).grid(row = 2,column=10,sticky = 'W')
tk.Label(window,text = "Infected", bg='Light Grey' ,fg = 'Orange',font = "none 14 bold").grid(row = 4,column = 0,sticky= 'W')
tk.Label(window,text = "Recovered", bg='Light Grey' ,fg = 'sea green',font = "none 14 bold").grid(row = 6,column = 0,sticky= 'W')
tk.Label(window,text = "Deceased", bg='Light Grey' ,fg = 'red3',font = "none 14 bold").grid(row = 8,column = 0,sticky= 'W')
def ifr():
inf = tk.Label(window,text = int((inf/dec)*100), bg='Orange' ,fg = 'Black',font = "none 14 bold").grid(row = 12,column = 6,sticky= 'W')
def cmr():
entered_text= variable.get()
print(entered_text)
Button(window,text = 'IFR',width = 6,command = ifr).grid(row = 10,column=2,sticky = 'W')
Button(window,text = 'CMR',width = 6,command = cmr).grid(row = 10,column=4,sticky = 'W')
tk.Label(window,text = "Value", bg='Light Grey' ,fg = 'RoyalBlue1',font = "none 14 bold").grid(row = 12,column = 0,sticky= 'W')
window.mainloop()
error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Himani Bali\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "<ipython-input-14-2509ea409cfd>", line 2, in ifr
inf = tk.Label(window,text = int((inf/dec)*100), bg='Orange' ,fg = 'Black',font = "none 14 bold").grid(row = 12,column = 6,sticky= 'W')
UnboundLocalError: local variable 'inf' referenced before assignment
您需要在函数中
global
:退出函数:
输出:
函数中的,
输出:
但是,使用
global
是一种不好的做法,因此最好避免它相关问题 更多 >
编程相关推荐