Tkinter get() 小部件和全局变量的名称错误
Name error with Tkinter get() widget and global variables
enter image description here我想要做的是让用户输入 4 个值到条目小部件,然后在另一个函数中使用这些输入值或函数 returns 但我得到名称错误。我试图将入口变量变成全局变量,因为它仍然给出名称错误。我下面的代码就像一个摘要,我是初学者所以有人可以帮忙吗?
这是错误:
---> 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: 名称 'sku' 未定义
***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
谢谢大家的评论!就像你建议的那样,我能够找到问题的根源并意识到我需要一种完全不同的方法并摆脱我所有的全局变量和单独的函数。相反,我创建了一个在 class 下有 4 个条目的函数,并且我使用 Prompt() 属性稍后在另一个函数中使用这些值。这是我的代码:
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()
enter image description here我想要做的是让用户输入 4 个值到条目小部件,然后在另一个函数中使用这些输入值或函数 returns 但我得到名称错误。我试图将入口变量变成全局变量,因为它仍然给出名称错误。我下面的代码就像一个摘要,我是初学者所以有人可以帮忙吗?
这是错误:
---> 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: 名称 'sku' 未定义
***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
谢谢大家的评论!就像你建议的那样,我能够找到问题的根源并意识到我需要一种完全不同的方法并摆脱我所有的全局变量和单独的函数。相反,我创建了一个在 class 下有 4 个条目的函数,并且我使用 Prompt() 属性稍后在另一个函数中使用这些值。这是我的代码:
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()