如何将 sql 数据库中的数据放入 python 中 tkinter 的组合框中?
How do I put data from sql database into a combobox in tkinter in python?
This is the database I want to call from
This is the empty combobox, When I press down nothing comes up
Label(self.root,
text="Supplier",
font=("Bahnschrift SemiBold",15),
bg="blue",
fg="white").place(x=0, y=325, height=40)
Supplier=ttk.Combobox(self.root,
postcommand=self.combo,
state="readonly",
font=("Bahnschrift SemiBold",15)).place(x=80, y=325, width=420, height=40
def add(self):
con=sqlite3.connect(database="product.db")
cur=con.cursor()
try:
if self.pid.get()=="":messagebox.showerror("Error","Product ID is required",parent=self.root)
else:
cur.execute("Select * from product where pid=?",(self.pid.get(),))
row=cur.fetchone()
if row!=None:
messagebox.showerror("Error","This Product ID is already assigned",parent=self.root)
else:
cur.execute("Insert into product(pid,name,supplier,price,quantity,description,total)values(?,?,?,?,?,?,?)",(
self.pid.get(),
self.name.get(),
self.supplier.get(),
self.price.get(),
self.quantity.get(),
self.description.get(),
self.price.get()*self.quantity.get(),
))
con.commit()
messagebox.showinfo("Success","Product added successfully",parent=self.root)
except Exception as e:
messagebox.showerror("Error",f"Error due to: {str(e)}",parent=self.root) ##
def combo(self):
conn=sqlite3.connect("supplier.db")
c = conn.cursor()
c.execute("SELECT name FROM supplier")
slist = c.fetchall()
values = [row[0] for row in slist]
self.supplier
con.commit()
con.close()
if __name__=="__main__":
root=Tk()
obj=productClass(root)
root.mainloop()
我想用在数据库中找到的结果填充组合框。然后将结果添加到新数据库中。
还有一个不同的问题,我如何调用数据库并将结果与用户输入进行比较。我想这样做来制作一个登录系统。我已经有了寄存器部分,但我对如何调用和比较数据库感到困惑。
希望这个问题不难。
**现在没有出现错误,但是组合框中没有显示来自数据库的结果。
错误:
AttributeError: 'StringVar' 对象没有属性 'configure'
您的 combo
函数正在从数据库中获取数据,但未对数据执行任何操作。您必须使用数据配置组合框。
第一步是确保可以引用组合框。假设此代码全部相同 class 您应该使组合框成为对象的属性:
self.supplier=ttk.Combobox(self.root, postcommand=self.combo, ...)
接下来,在您的 combo
函数中,您需要设置组合框的 values
属性:
def combo(self):
...
slist = c.fetchall()
self.supplier.configure(values=slist)
...
我不记得在选择一列时 fetchall
returns 是平面列表还是元组列表。如果它是元组列表,您需要在将其分配给值之前将其展平,也许是这样的:
values = [row[0] for row in slist]
self.supplier.configure(values=values)
重点是,通过 postcommand
属性调用的函数负责更新值。
This is the database I want to call from This is the empty combobox, When I press down nothing comes up
Label(self.root,
text="Supplier",
font=("Bahnschrift SemiBold",15),
bg="blue",
fg="white").place(x=0, y=325, height=40)
Supplier=ttk.Combobox(self.root,
postcommand=self.combo,
state="readonly",
font=("Bahnschrift SemiBold",15)).place(x=80, y=325, width=420, height=40
def add(self):
con=sqlite3.connect(database="product.db")
cur=con.cursor()
try:
if self.pid.get()=="":messagebox.showerror("Error","Product ID is required",parent=self.root)
else:
cur.execute("Select * from product where pid=?",(self.pid.get(),))
row=cur.fetchone()
if row!=None:
messagebox.showerror("Error","This Product ID is already assigned",parent=self.root)
else:
cur.execute("Insert into product(pid,name,supplier,price,quantity,description,total)values(?,?,?,?,?,?,?)",(
self.pid.get(),
self.name.get(),
self.supplier.get(),
self.price.get(),
self.quantity.get(),
self.description.get(),
self.price.get()*self.quantity.get(),
))
con.commit()
messagebox.showinfo("Success","Product added successfully",parent=self.root)
except Exception as e:
messagebox.showerror("Error",f"Error due to: {str(e)}",parent=self.root) ##
def combo(self):
conn=sqlite3.connect("supplier.db")
c = conn.cursor()
c.execute("SELECT name FROM supplier")
slist = c.fetchall()
values = [row[0] for row in slist]
self.supplier
con.commit()
con.close()
if __name__=="__main__":
root=Tk()
obj=productClass(root)
root.mainloop()
我想用在数据库中找到的结果填充组合框。然后将结果添加到新数据库中。
还有一个不同的问题,我如何调用数据库并将结果与用户输入进行比较。我想这样做来制作一个登录系统。我已经有了寄存器部分,但我对如何调用和比较数据库感到困惑。
希望这个问题不难。
**现在没有出现错误,但是组合框中没有显示来自数据库的结果。
错误: AttributeError: 'StringVar' 对象没有属性 'configure'
您的 combo
函数正在从数据库中获取数据,但未对数据执行任何操作。您必须使用数据配置组合框。
第一步是确保可以引用组合框。假设此代码全部相同 class 您应该使组合框成为对象的属性:
self.supplier=ttk.Combobox(self.root, postcommand=self.combo, ...)
接下来,在您的 combo
函数中,您需要设置组合框的 values
属性:
def combo(self):
...
slist = c.fetchall()
self.supplier.configure(values=slist)
...
我不记得在选择一列时 fetchall
returns 是平面列表还是元组列表。如果它是元组列表,您需要在将其分配给值之前将其展平,也许是这样的:
values = [row[0] for row in slist]
self.supplier.configure(values=values)
重点是,通过 postcommand
属性调用的函数负责更新值。