尝试调用函数提交到 MySQL 时如何修复“未定义变量”
Posted
技术标签:
【中文标题】尝试调用函数提交到 MySQL 时如何修复“未定义变量”【英文标题】:How to fix ' variable not defined' when trying to call a function to submit to MySQL 【发布时间】:2019-08-31 03:23:27 【问题描述】:我是 Tkinter 的新手,并试图创建一个函数来连接到我的 GUI 中的“提交”按钮,以便它将数据输入到我的 mysql 数据库中。反馈告诉我'itemCode_entry' is not defined
在def inv_submit():
函数中。我正在尝试从def inv_menu():
函数中获取来自itemCode_entry
的输入,并将其输入到MySQL 语句中的def inv_submit():
。
这是我得到的反馈。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\darre\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "Import_Test_Code2.py", line 103, in <lambda>
submit_btn = Button(btm_frame, text='Submit', bg=color1, command= lambda : inv_submit())
File "Import_Test_Code2.py", line 207, in inv_submit
item = itemCode_entry.get()
NameError: name 'itemCode_entry' is not defined
from tkinter import *
import mysql.connector
# ======================MySQL Connection================================================================
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "....",
database = "testdb",
)
# Create Cursor Instance
my_cursor = mydb.cursor()
# Create Database
#my_cursor.execute("CREATE DATABASE trialProjectdb")
root = Tk()
root.geometry('500x500')
root.title('Database Control')
color1 = 'grey77'
color2 = 'grey88'
item = int()
brandName = StringVar()
#unitsPer = int()
units = int()
cost = float()
# ======================Frames================================================================
top_frame = Frame(root, width=500, height=80, bg=color1)
top_frame.pack(side=TOP)
btm_frame = Frame(root, width=500, height=500, bg=color2)
btm_frame.pack(side=TOP)
# ======================Inventory=============================================================
inv_btn = Button(top_frame, text='Inventory', width=20,
command= lambda : inv_menu())
inv_btn.place(x=0, y=0)
# ======================Functions=============================================================
def inv_menu():
menu_label = Label(btm_frame, text='Inventory', font=('arial', 12, 'bold'), bg=color2)
menu_label.place(x=200, y=10)
line = Label(btm_frame, text='______________________________________________________'
'________________________________________________', bg=color2)
line.place(x=0, y=30)
itemCode_label = Label(btm_frame, text='Item Code:', bg=color2)
itemCode_label.place(x=22, y=60)
itemCode_entry = Entry(btm_frame, textvariable=item)
itemCode_entry.place(x=90, y=60)
brand_label = Label(btm_frame, text='Brand:', bg=color2)
brand_label.place(x=45, y=90)
brand_entry = Entry(btm_frame, textvariable=brandName)
brand_entry.place(x=90, y=90)
units_label = Label(btm_frame, text='Units Per:', bg=color2)
units_label.place(x=28, y=120)
units_entry = Entry(btm_frame, textvariable=units)
units_entry.place(x=90, y=120)
unitCost_label = Label(btm_frame, text='Unit Cost:', bg=color2)
unitCost_label.place(x=28, y=150)
unitCost_entry = Entry(btm_frame, textvariable=cost)
unitCost_entry.place(x=90, y=150)
submit_btn = Button(btm_frame, text='Submit', bg=color1, command= lambda : inv_submit())
submit_btn.place(x=90, y=180)
def inv_submit():
item = itemCode_entry.get()
brandName = brand_entry.get()
units = units_entry.get()
cost = unitCost_entry.get()
my_cursor.execute("CREATE TABLE IF NOT EXIST 'trialprojectdb'.'Inventory' (Item_Code INTEGER AUTO_INCREMENT PRIMARY KEY , Brand VARCHAR(255), Units INTEGER(10), In_Stock INTEGER, Unit_Cost FLOAT(12,2)")
my_cursor.execute("INSERT INTO Inventory (itemCode, brand, unitsPer, unitCost) VALUES(%s,%s,%s,%s)", (item, brandName, units, cost))
mydb.commit()
conn.close()
root.mainloop()
一旦我填写了“库存”表单inv_menu()
中的信息,我希望“提交”按钮submit_btn
将数据提交到MySQL 以更新数据库。
【问题讨论】:
itemCode 到底应该是什么? itemCode_entry = Entry(btm_frame, textvariable=itemCode) "itemCode" 没有在任何地方定义 @alex067 抱歉。我尝试了很多方法来修复它,以至于我输入了错误的代码。我刚刚对代码和输出进行了编辑。谢谢。 What are the rules for local and global variables in Python? 【参考方案1】:使其成为全局条目小部件:
global itemCode_entry
itemCode_entry = Entry(btm_frame, textvariable=item)
itemCode_entry.place(x=90, y=60)
【讨论】:
【参考方案2】:您遇到的问题是范围界定。例如,item = itemCode_entry.get()
在您的 inv_submit() 函数中。这将导致一个问题,因为在“inv_submit()”函数的范围内或程序的全局范围内都找不到“itemCode_entry.get()”。
一个可能的解决方案是这样的:
来自
dev inv_menu():
...
return itemCode_entry, brand_entry, units_entry, unitCost_entry
在你的
dev inv_submit():
item, brandName, units, cost = inv_menu()
【讨论】:
以上是关于尝试调用函数提交到 MySQL 时如何修复“未定义变量”的主要内容,如果未能解决你的问题,请参考以下文章
如何修复 React 中的“类型错误:尝试访问对象的属性时无法读取未定义的属性名称”
如何修复此错误:调用未定义的函数 odbc_connect() [重复]