Я новичок в Tkinter и пытаюсь создать функцию, которая будет подключаться к моей кнопке «Отправить» в моем графическом интерфейсе, чтобы она вводила данные в мою базу данных MySQL.Обратная связь говорит мне, что 'itemCode_entry' is not defined
в функции def inv_submit():
.Я пытаюсь получить входные данные из itemCode_entry
из функции def inv_menu():
и ввести их в def inv_submit():
в операторе MySQL.
Это обратная связь, которую я получаю.
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()
Я ожидаю, что кнопка «Отправить» submit_btn
отправит данные в MySQL для обновления базы данных после того, как я заполнил информацию в форме «Инвентаризация» inv_menu()
.