UnboundLocalError:赋值前引用了局部变量“inf”

2024-05-01 21:26:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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

Tags: textnonedatacolumnwindowvariablelabeltk
1条回答
网友
1楼 · 发布于 2024-05-01 21:26:53

I have used global also but than error is coming again.

您需要在函数中global

退出函数:

global x

def add():
    x += 1
    
x = 0
add()

print(x)

输出:

UnboundLocalError: local variable 'x' referenced before assignment

函数中的

def add():
    global x
    x += 1

x = 0
add()

print(x)

输出:

1

但是,使用global是一种不好的做法,因此最好避免它

相关问题 更多 >