I Want To Populate A Ttk.combobox With Results From Database Query
i am having a little problem i would need help with.This concerns python ttk.combobox widget I am new to python just started coding a week ago. I have a database which is populated
Solution 1:
First: you forgot ()
to execute function
self.comb['value'] = self.combo_input()
Second: inside combo_input
you use return row
so you return only first row, nothing more.
You have to create Python list with all values and then return it. It can be something like this:
def combo_input(self):
db = sqlite3.connect('stockdbExample.db')
cursor= db.execute('select item from stocks')
result= []
forrowin cursor.fetchall():
result.append(row[0])
returnresult
EDIT: Full, working example:
import tkinter as tk
import tkinter.ttk as ttk
import sqlite3
classExample:
def__init__(self,master):
self.master = master
self.db = sqlite3.connect('stockdbExample.db')
# use only once
self.create_db()
self.cb = ttk.Combobox(master)
self.cb.pack()
self.cb['values'] = self.combo_input()
defcombo_input(self):
cursor = self.db.cursor()
cursor.execute('SELECT item FROM stocks')
data = []
for row in cursor.fetchall():
data.append(row[0])
return data
defcreate_db(self):
cursor = self.db.cursor()
cursor.execute('CREATE TABLE stocks (item text)')
cursor.execute('INSERT INTO stocks (item) VALUES("Hello")')
cursor.execute('INSERT INTO stocks (item) VALUES("World")')
cursor.execute('INSERT INTO stocks (item) VALUES("Tkinter")')
cursor.close()
self.db.commit()
root = tk.Tk()
Example(root)
root.mainloop()
Post a Comment for "I Want To Populate A Ttk.combobox With Results From Database Query"