You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Tkinter供应商模块问题:输入Supplier ID仍提示必填

库存管理系统Supplier ID校验问题排查与修复

问题根源

你编写的代码中,Supplier ID输入框未与绑定的StringVar变量关联,导致输入内容无法同步到self.var_sup_id中。在add()方法里调用self.var_sup_id.get()始终返回空字符串,因此无论输入什么内容,都会触发非空校验错误。

同时,Name和Contact输入框也存在相同问题,未绑定对应的self.var_name和self.var_contact,即使修复ID校验,插入数据库时也会存入空值。

修复方案

为每个输入框添加textvariable参数,绑定对应的类成员变量:

修改输入框代码

将原输入框创建代码:

txt_supid=Entry(frame1,font=('calibri',15),bg='white').place(x=370,y=130,width=250)
txt_name=Entry(frame1,font=('calibri',15),bg='white').place(x=50,y=130,width=250)
txt_contact=Entry(frame1,font=('calibri',15),bg='white').place(x=700,y=130,width=250)

替换为:

# 绑定Supplier ID输入框到self.var_sup_id
txt_supid=Entry(frame1,textvariable=self.var_sup_id,font=('calibri',15),bg='white').place(x=370,y=130,width=250)
# 绑定Name输入框到self.var_name
txt_name=Entry(frame1,textvariable=self.var_name,font=('calibri',15),bg='white').place(x=50,y=130,width=250)
# 绑定Contact输入框到self.var_contact
txt_contact=Entry(frame1,textvariable=self.var_contact,font=('calibri',15),bg='white').place(x=700,y=130,width=250)

额外优化(可选)

当前代码中Entry(...).place()会将变量赋值为None(因为place()返回None),如果后续需要操作输入框(比如清空内容),建议拆分创建与布局:

txt_supid=Entry(frame1,textvariable=self.var_sup_id,font=('calibri',15),bg='white')
txt_supid.place(x=370,y=130,width=250)

txt_name=Entry(frame1,textvariable=self.var_name,font=('calibri',15),bg='white')
txt_name.place(x=50,y=130,width=250)

txt_contact=Entry(frame1,textvariable=self.var_contact,font=('calibri',15),bg='white')
txt_contact.place(x=700,y=130,width=250)

完整修复后的代码

from tkinter import* 
from PIL import Image,ImageTk
from tkinter import ttk,messagebox
import sqlite3 

class supplierClass:
    def __init__(self,root):
        self.root=root
        self.root.geometry('1100x500+220+130')
        self.root.title('Stock Inventory Management System')
        self.root.config(bg='white')
        self.root.focus_force()

        self.var_searchby=StringVar()
        self.var_searchtxt=StringVar()

        self.var_sup_id=StringVar()
        self.var_name=StringVar()
        self.var_contact=StringVar()


        #Search Function
        SearchFunction=LabelFrame(self.root,text='Search Supplier',font=('Gill Sans MT',12,'bold'),bd=2,relief=RIDGE,bg='white')
        SearchFunction.place(x=600,y=20,width=600,height=70)

        cmb_search=ttk.Combobox(SearchFunction,textvariable=self.var_searchby,values=('Select','Name','ID','Contact'),state='readonly',justify=CENTER,font=('Gills Sans MT',15))
        cmb_search.place(x=10,y=10,width=180)
        cmb_search.current(0)

        txt_search=Entry(SearchFunction,textvariable=self.var_searchtxt,font=('Gill Sans MT',15),bg='white').place(x=200,y=10)
        txt_search=Button(SearchFunction,text='Search',font=('Gill Sans MT',15),fg='white',bg='blue',cursor='hand2').place(x=410,y=11,width=150,height=30)

        #Title
        title=Label(self.root,text='Supplier Details',font=('calibri',15),bg='black',fg='white').place(x=400,y=100,width=1000)

        #Frame
        frame1=Frame(self.root,bg='silver')
        frame1.place(x=400,y=130,width=1000,height=500)

        #Content
        lbl_supid=Label(frame1, text='Supplier ID',font=('calibri',15,'bold'),bg='silver',fg='black').place(x=50,y=100)
        # 绑定Supplier ID输入框到self.var_sup_id
        txt_supid=Entry(frame1,textvariable=self.var_sup_id,font=('calibri',15),bg='white').place(x=370,y=130,width=250)

        lbl_name=Label(frame1, text='Name',font=('calibri',15,'bold'),bg='silver',fg='black').place(x=370,y=100)
        # 绑定Name输入框到self.var_name
        txt_name=Entry(frame1,textvariable=self.var_name,font=('calibri',15),bg='white').place(x=50,y=130,width=250)

        lbl_contact=Label(frame1, text='Contact',font=('calibri',15,'bold'),bg='silver',fg='black').place(x=700,y=100)
        # 绑定Contact输入框到self.var_contact
        txt_contact=Entry(frame1,textvariable=self.var_contact,font=('calibri',15),bg='white').place(x=700,y=130,width=250)

        #Buttons
        btn_add=Button(self.root,text='Save',command=self.add,font=('Gill Sans MT',15),fg='white',bg='green',cursor='hand2').place(x=500,y=315,width=110,height=28)
        btn_update=Button(self.root,text='Update',font=('Gill Sans MT',15),fg='white',bg='purple',cursor='hand2').place(x=620,y=315,width=110,height=28)
        btn_delete=Button(self.root,text='Delete',font=('Gill Sans MT',15),fg='white',bg='red',cursor='hand2').place(x=740,y=315,width=110,height=28)
        btn_clear=Button(self.root,text='Clear',font=('Gill Sans MT',15),fg='white',bg='grey',cursor='hand2').place(x=860,y=315,width=110,height=28)
       

        #Supplier Details
        sup_frame=Frame(self.root,bd=3,relief=RIDGE)
        sup_frame.place(x=400,y=400,relwidth=0.521,height=230)

        scrolly=Scrollbar(sup_frame,orient=VERTICAL)
        scrollx=Scrollbar(sup_frame,orient=HORIZONTAL)

        self.SupplierTable=ttk.Treeview(sup_frame,columns=('supid','name','contact'),yscrollcommand=scrolly.set,xscrollcommand=scrollx.set)
        scrollx.pack(side=BOTTOM,fill=X)
        scrolly.pack(side=RIGHT,fill=Y)
        scrollx.config(command=self.SupplierTable.xview)
        scrolly.config(command=self.SupplierTable.yview)

        
        self.SupplierTable.heading('supid',text='SUP ID')
        self.SupplierTable.heading('name',text='NAME')
        self.SupplierTable.heading('contact',text='CONTACT')
        
        self.SupplierTable['show']='headings'
        
        self.SupplierTable.column('supid',width=90)
        self.SupplierTable.column('name',width=100)
        self.SupplierTable.column('contact',width=100)

        self.SupplierTable.pack(fill=BOTH,expand=1)

    #Functions
    
    def add(self):
        con=sqlite3.connect(database=r'sims.db')
        cur=con.cursor()
        try:
            if self.var_sup_id.get()=='':
                messagebox.showerror('Error','Supplier ID must be required',parent=self.root)
            else:
                cur.execute('Select * from supplier where supid=?',(self.var_sup_id.get(),))
                row=cur.fetchone()
                if row!=None:
                    messagebox.showerror('Error', 'This Supplier ID is already assigned, try a different one',parent=self.root)
                else:
                    cur.execute('Insert into supplier (supid,name,contact) values(?,?,?)',(
                                        self.var_sup_id.get(),
                                        self.var_name.get(),
                                        self.var_contact.get()      
                    ))
                    con.commit()
                    messagebox.showinfo('Success','Supplier added successfully',parent=self.root)                    
        except Exception as ex:
            messagebox.showerror('Error',f'Error due to : {str(ex)}',parent=self.root)


if __name__=='__main__':
    root=Tk()
    obj=supplierClass(root)
    root.mainloop()

内容的提问来源于stack exchange,提问作者imwajih

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 03:55:16