這是我想從中呼叫的資料庫 這是空的組合框,當我按下時什么都沒有出現
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”物件沒有屬性“配置”
uj5u.com熱心網友回復:
您的combo函式正在從資料庫中獲取資料,但不對資料執行任何操作。您必須使用資料配置組合框。
您的第一步是確保您可以參考組合框。假設此代碼都在同一個類中,您應該使組合框成為物件的屬性:
self.supplier=ttk.Combobox(self.root, postcommand=self.combo, ...)
接下來,在您的combo函式中,您需要設定values組合框的屬性:
def combo(self):
...
slist = c.fetchall()
self.supplier.configure(values=slist)
...
我不記得fetchall在選擇一列時是否回傳平面串列或元組串列。如果它是一個元組串列,您需要在將其分配給值之前將其展平,可能是這樣的:
values = [row[0] for row in slist]
self.supplier.configure(values=values)
關鍵是,通過屬性呼叫的函式postcommand負責更新值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435150.html
標籤:Python sql 数据库 sqlite tkinter
