Home > Blockchain >  How do I put data from sql database into a combobox in tkinter in python?
How do I put data from sql database into a combobox in tkinter in python?

Time:03-01

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()

I want to populate the combo box with results found in a database. Then add the results from this into a new database.

Also a different question how can I call to a database and compare the results to the user input. I want to do this to make a login system. I already have the register part but I'm confused on how to call and compare to the database.

Hopefully this question isn't to hard.

**No error now comes up but no results from the database is showing up on the combo box.

Error: AttributeError: 'StringVar' object has no attribute 'configure'

CodePudding user response:

Your combo function is fetching data from the database, but doesn't do anything with the data. You must configure the combobox with the data.

Your first step is to make sure you can reference the combobox. Assuming this code is all in the same class you should make the combobox an attribute of the object:

self.supplier=ttk.Combobox(self.root, postcommand=self.combo, ...)

Next, in your combo function you need to set the values attribute of the combobox:

def combo(self):
    ...
    slist = c.fetchall()
    self.supplier.configure(values=slist)
    ...

I don't remember if fetchall returns a flat list or a list of tuples when selecting one column. If it's a list of tuples you'll need to flatten it out before assigning it to values, perhaps with something like this:

values = [row[0] for row in slist]
self.supplier.configure(values=values)

The point is, the function called via the postcommand attribute is responsible for updating the values.

  • Related