简体   繁体   中英

NameError: name 'variable' is not defined - tkinter

I am working on an assignment which involves working with openpyxl, tkinter and pandas. I need to do some calculations in excel based on one input (avg_trips)

This is the current code. After uploading the file the code just asks for input in the terminal. but I want it to ask for input in the tkinter window

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: "))

This is the tkinter code that I tried:

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()

I gave the same variable name (avg_trips) but I am getting this error:

NameError: name 'avg_trips' is not defined

How do I make it where the input is asked in the tkinter file and the button triggers the rest of the process?

Change this:

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

to this:

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())

Also you have to change this:

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: "))

to this:

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: "))

A mutable object is an object that you can change like a list. Immutable are objects that you can't change like tuple / str / int . Python hides most of that from the user but that can some times confuse people. The basic rule is that the only mutable built in object is a list - all other built in objects are immutable. For more info about mutable vs immutable object read this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM