Python UnboundLocalError:局部变量';inf&x27;分配前参考

Python UnboundLocalError:局部变量';inf&x27;分配前参考,python,tkinter,drop-down-menu,Python,Tkinter,Drop Down Menu,我正在尝试使用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("P

我正在尝试使用
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
从tkinter导入*
将tkinter作为tk导入
作为pd进口熊猫
data=pd.read\u csv('STATE\u WISE\u COVID\u data.csv'))
window=tk.tk()
窗口几何(“500x500”)
窗口标题(“流行病分析引擎”)
window.configure(背景=“浅灰色”)
标签(窗口,text=“流行病分析仪”,bg='浅灰色',fg='深蓝色',font=“无14粗体”).grid(行=0,列=0,粘性='W')
tk.Label(窗口,text=“选择状态”,bg='浅灰色',fg='白色',font=“无14粗体”).grid(行=2,列=0,粘性='W')
变量=StringVar(窗口)
变量.set(数据.State[0])
w=选项菜单(窗口、变量、*data.State)
w、 网格(行=2,列=6,粘性=w')
def click():
全局inf
全球记录
全球十二月
inf=tk.Label(window,text=int(data[data.State==variable.get()].confirm),bg='Orange',fg='Black',font=“none 14 bold”).grid(行=4,列=6,粘性='W')
rec=tk.Label(window,text=int(data[data.State==variable.get()].Recovered),bg='Orange',fg='Black',font=“none 14 bold”).grid(行=6,列=6,粘性='W')
dec=tk.Label(window,text=int(data[data.State==variable.get()].deased),bg='Orange',fg='Black',font=“none 14 bold”).grid(行=8,列=6,粘性='W')
按钮(窗口,文本=OK,宽度=6,命令=click)。网格(行=2,列=10,粘性=W)
标签(window,text=“Infected”,bg='浅灰色',fg='橙色',font=“none 14 bold”).grid(行=4,列=0,粘性='W')
tk.Label(窗口,text=“Recovered”,bg='浅灰色',fg='海绿色',font=“none 14 bold”).grid(行=6,列=0,粘性='W')
tk.Label(窗口,text=“已故”,bg=”浅灰色”,fg=”红色3',font=“无14粗体”).grid(行=8,列=0,粘性=”W')
def ifr():
inf=tk.Label(窗口,text=int((inf/dec)*100),bg='Orange',fg='Black',font='none 14 bold').grid(行=12,列=6,粘性='W')
def cmr():
已输入\u text=variable.get()
打印(输入的文本)
按钮(窗口,文本='IFR',宽度=6,命令=IFR)。网格(行=10,列=2,粘性='W')
按钮(窗口,文本=CMR,宽度=6,命令=CMR)。网格(行=10,列=4,粘性=W)
tk.Label(窗口,text=“Value”,bg='浅灰色',fg='RoyalBlue1',font=“none 14 bold”).grid(行=12,列=0,粘性='W')
window.mainloop()
错误:
Tkinter回调中的异常
回溯(最近一次呼叫最后一次):
文件“C:\Users\Himani-Bali\Anaconda3\lib\tkinter\\uuuuu init\uuuuu.py”,第1705行,在调用中__
返回self.func(*args)
ifr中第2行的文件“”
inf=tk.Label(窗口,text=int((inf/dec)*100),bg='Orange',fg='Black',font='none 14 bold').grid(行=12,列=6,粘性='W')
UnboundLocalError:赋值前引用了局部变量“inf”
我也用过全局的,但是错误又来了

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

退出函数:

输出:

UnboundLocalError: local variable 'x' referenced before assignment
1
在函数中:

输出:

UnboundLocalError: local variable 'x' referenced before assignment
1

但是使用
全局
是一种不好的做法,因此最好避免这种做法。

您提到您会遇到错误。请附上错误日志。如果没有错误日志,则无法调试Tkinter回调回溯中的异常(最近一次调用最后一次):文件“C:\Users\Himani Bali\Anaconda3\lib\Tkinter_init_u2;.py”,第1705行,在调用返回self.func(*args)文件“”中,第2行,在ifr inf=tk.Label(窗口,text=int((inf/dec)*100),bg='Orange',fg='Black',font=“无14粗体”).grid(行=12,列=6,sticky='W')UnboundLocalError:在赋值之前引用了局部变量'inf'。谢谢,我尝试了,但又出现了相同的错误def click():global inf global rec global dec代替
global inf global rec global dec
,使用
global inf,rec,dec
。调用返回self.func(*args)文件“2”第1705行中的文件“C:\Users\Himani Bali\Anaconda3\lib\Tkinter_init.py”异常,在ifr inf=tk.Label(窗口,text=int((inf/dec)*100),bg='Orange',fg='Black',font='none 14 bold').grid(row=12,column=6,sticky=W')中,UnboundLocalError:赋值前引用了局部变量'inf'