Search code examples
pythonglobal-variablestkinter-entry

Name error with Tkinter get() widget and global variables


enter image description hereWhat I want to do is get user to input 4 values to entry widgets and after and using those input values or function returns in another function but I get name error. I tried to turn the entry variables into global because but it still gives name error. My code below is like a summary and I am a beginner so can anyone help?

This is the error:

---> 96 user_input = {'SKU':sku, 'From Company Descp.':SupplyOrigin, 'To Company Descp.':FinalLocation , 'Incoterms':Incoterm} 97 inputDf = pd.DataFrame(pd.Series(user_input)).T 98 #inputDf = pd.DataFrame(user_input)

NameError: name 'sku' is not defined

***CODE HERE***
    import pandas as pd
    import numpy as np
    import tkinter as tk
    from tkinter import filedialog, messagebox, ttk
    import pandas as pd
    from tkinter import *

# initalise the tkinter GUI
root = tk.Tk()

#creating entry widgets for all 4 input areas that I want to get from the user
frame2 = tk.LabelFrame(root, text="User Input") #second frame will be used to put the entry widgets inside it
frame2.place(height=120, width=600, rely=0.75, relx=0.01)

canvas1=tk.Canvas(frame2, height=100, width=600) #create a canvas to inside the frame
entry1 = tk.Entry(root)
canvas1.create_window(60,50, window=entry1)
canvas1.pack()
label1 = tk.Label(frame2, text= "Enter SKU:", bg="purple", fg="white")
label1.place(rely=0.1,relx=0.0)

entry2 = tk.Entry(root) #creating a new entry for supply origin
canvas1.create_window(200, 50, window=entry2)
label2 =tk.Label(frame2, text= "Supply Origin Country:", bg="purple", fg="white")
label2.place(rely=0.1,relx=0.23)

entry3 = tk.Entry(root) #creating a new entry for final location
canvas1.create_window(340, 50, window=entry3)
label3 =tk.Label(frame2, text= "Final Location Country:", bg="purple", fg="white")
label3.place(rely=0.1,relx=0.47)


entry4 = tk.Entry(root) #creating a new entry for incoterm
canvas1.create_window(480, 50, window=entry4)
label4 =tk.Label(frame2, text= "Incoterm:", bg="purple", fg="white")
label4.place(rely=0.1,relx=0.7)

#Here I defined four functions to get inputs from user and I wanted to use the return values inside another function

def SKU():
    global sku
    sku = entry1.get() #get sku number 
    return sku

def Supply_Origin():
    global SupplyOrigin
    SupplyOrigin =entry2.get()
    return SupplyOrigin


def Final_Location():
    global FinalLocation
    FinalLocation = entry3.get()
    return FinalLocation


def Inc():
    global Incoterm
    Incoterm = entry4.get()
    return Incoterm
    #Inc = entry4.get() #get incoterm
    #Incoterm = []
    #Incoterm.append(Inc)

def user_input_calculator():
"I want to use global variables or above function return values in here"
    user_input = {'SKU':sku, :SupplyOrigin,'To Company Descp.':FinalLocation, 'To Company Descp.':FinalLocation , 'Incoterms':Incoterm}
    inputDf = pd.DataFrame(pd.Series(user_input)).T
    return inputDf

Solution

  • Thank you for the comments guys! Like you suggested I was able to get to the root of the problem and realized I needed an entirely different approach and got rid of all my global variables and seperate functions. Instead I created one function that has 4 entries under a class and I used the Prompt() attribute to use those values later on in another function. Here is my code:

    import tkinter as tk
    from tkinter import filedialog, messagebox, ttk
    import pandas as pd
    from tkinter import *
    
    
    # initalise the tkinter GUI
    root = tk.Tk()
    
    #forming the frame beforehand
    frame2 = tk.LabelFrame(root, text="User Input") #second frame will be used to put the entry widgets inside it
    frame2.place(height=120, width=600, rely=0.75, relx=0.01)
    
    canvas1=tk.Canvas(frame2, height=100, width=600) #create a canvas to inside the frame
    entry1 = tk.Entry(root)
    canvas1.create_window(60,50, window=entry1)
    canvas1.pack()
    label1 = tk.Label(frame2, text= "Enter SKU:", bg="purple", fg="white")
    label1.place(rely=0.1,relx=0.0)
    
    entry2 = tk.Entry(root) #creating a new entry for supply origin
    canvas1.create_window(200, 50, window=entry2)
    label2 =tk.Label(frame2, text= "Supply Origin Country:", bg="purple", 
    fg="white")
    label2.place(rely=0.1,relx=0.23)
    
    entry3 = tk.Entry(root) #creating a new entry for final location
    canvas1.create_window(340, 50, window=entry3)
    label3 =tk.Label(frame2, text= "Final Location Country:", bg="purple", 
    fg="white")
    label3.place(rely=0.1,relx=0.47)
    
    
    entry4 = tk.Entry(root) #creating a new entry for incoterm
    canvas1.create_window(480, 50, window=entry4)
    label4 =tk.Label(frame2, text= "Incoterm:", bg="purple", fg="white")
    label4.place(rely=0.1,relx=0.7)
    
    button5 = tk.Button(frame2, text='Load input', command=prompt.user_input, bg='green', fg='white', font=('helvetica', 10, 'bold'))
    button5.place(rely=0.50, relx=0.15)
    canvas1.create_window(60, 80, window=button5)
    
    class Prompt:
        def user_input(self):
            self.a = entry1.get() #these are the variables that I will use in user_input_calculator function later on using prompt attribute
            self.b = entry2.get()
            self.c = entry3.get()
            self.d = entry4.get()
            
        def __init__(self, root):
            self.btn = tk.Button(root, text="Get your input", command=self.user_input)
            self.btn.place(rely=0.50, relx=0.40)
            self.btn.pack()
    prompt = Prompt(root)
    
    
    def user_input_calculator():
    
    user_input = {'SKU':prompt.a, 'From Company Descp.':prompt.b, 'To Company Descp.':prompt.c , 'Incoterms':prompt.d}
        inputDf = pd.DataFrame(pd.Series(user_input)).T
        return inputDf
    root.mainloop()