I want to populate a ttk.combobox with results from database query

谁都会走 提交于 2019-12-25 08:49:25

问题


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 by user input.This database is to store a user's stock input(e.g. items, cost, supplier ,etc).This part i have been able to do.

From the database, am using a 'for' loop to get each seperate item from my database query into a ttk.combobox so a user can make selections. However my problem is,i get an error everytime. I want the combobox to show all items within my item column from my database query.

From online sources ,i found out an example for ttk.combobox:

    list = ['shoe', 'toy', 'bag']
    combobox = ttk.Combobox(root)
    combobox['values'] = list

This works fine,if i am to use a self-made list However , i can't find an example for a ttk.combobox using query from sqlite3 in python

Thank you in advance for helping me out with this problem. An example of how to do it and an explanation of what i did wrong will help me alot. Forgive me for my poorly organised coding style,i hope to get better at it as time goes on.

from tkinter import *
from tkinter import ttk
import sqlite3

class Example:
    def __init__(self,master):
     self.master = master
     self.win_label = Label(master, text="New Stock Entry")
     self.win_label.grid(row=0, columnspan=2,)
     self.item_label = Label(master, text="Item Name", fg='black')
     self.item_label.grid(row=1, column=0, sticky=W, padx=5, pady=5)
     self.unitprice_lab = Label(master, text="Unit Price", fg='black')
     self.unitprice_lab.grid(row=2, column=0, sticky=W, padx=5, pady=5)
     self.total_price = Label(master, text="Total Price", fg='black')
     self.total_price.grid(row=3, column=0, sticky=W, padx=5, pady=5)
     self.quantity_lab = Label(master, text="Quantity", fg='black')
     self.quantity_lab.grid(row=4, column=0, sticky=W, padx=5, pady=5)
     self.manufacturer_lab = Label(master,text="Manufacturer", fg='black')
     self.manufacturer_lab.grid(row=5, column=0, sticky=W, padx=5, pady=5)

      ############### Variables to store input ################
     self.item = StringVar()
     self.unitp = IntVar()
     self.unitn = IntVar()
     self.quantity = IntVar()
     self.man = StringVar()

     ############ Widgets############
     self.item_entry = Entry(master, width=25,textvariable=self.item)
     self.item_entry.grid(row=1, column=1, padx=5, pady=5)
     self.unitprice_entry = Entry(master, width=25,textvariable=self.unitp)
     self.unitprice_entry.grid(row=2, column=1, sticky=W, padx=5, pady=5)
     self.total_price_entry = Entry(master,width=25,textvariable=self.unitn)
     self.total_price_entry.grid(row=3, column=1, sticky=W, padx=5, pady=5)
     self.quantity_entry = Entry(master,width=25,textvariable=self.quantity)
     self.quantity_entry.grid(row=4, column=1, sticky=W, padx=5, pady=5)
     self.manufacturer_entry = Entry(master, width=25,textvariable=self.man)
     self.manufacturer_entry.grid(row=5, column=1, sticky=W, padx=5, pady=5)
     # button for save
     self.button_save = Button(master,text="Save",command= self.insert_Dbs)
     self.button_save.grid(row=6, column=1, pady=15, padx=10, sticky=W)

  def second_window(self):  # Second Window to allow user selection
    t = Toplevel()
    t.geometry('350x290')
    t.title('Student Input')

    ############# widgets ###############
    self.label1 = ttk.Label(t, text = 'Student Name')
    self.label1.grid(row =0 , column =0)
    self.label2 = ttk.Label(t, text='Item Collected')
    self.label2.grid(row=0, column=1)
    self.label3 = ttk.Label(t, text='Quantity')
    self.label3.grid(row=0, column=2)
    self.entry1 =ttk.Entry(t, width = 20)
    self.entry1.grid(row =1 , column =0)
    self.entry2 = ttk.Entry(t, width=20)
    self.entry2.grid(row=1, column=2)
    self.comb = ttk.Combobox(t,width = 15).grid(row =1 , column =1)
    self.comb['value'] = self.combo_input


  def insert_Dbs(self): # Function to insert input into database

    self.a1 = self.item.get()
    self.a2 = self.unitp.get()
    self.a3 = self.unitn.get()
    self.a4 = self.quantity.get()
    self.a5 = self.man.get()

    #db = sqlite3.connect('stockdbExample.db')
    '''db.execute('create table stocks (item text '
               ', item_uprice integer,'
               '  item_nprice integer,'
               ' quantity integer,'
               ' manufacturer text )')'''
    db = sqlite3.connect('stockdbExample.db')
    db.execute('insert into stocks (item,'
               ' item_uprice,'
               ' item_nprice,'
               ' quantity,'
               ' manufacturer) values (?, ?, ?, ?,?)',
               (self.a1, self.a2, self.a3, self.a4, self.a5))
    db.commit()
    self.combo_input()
    self.second_window()

  def combo_input(self):
    db = sqlite3.connect('stockdbExample.db')
    cursor = db.execute('select item from stocks')
    for row in cursor.fetchall():
        return row

root = Tk()
c = Example(root)
root.mainloop()

回答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 = []

    for row in cursor.fetchall():
        result.append(row[0])

    return result

EDIT: Full, working example:

import tkinter as tk
import tkinter.ttk as ttk
import sqlite3

class Example:

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

  def combo_input(self):
    cursor = self.db.cursor()

    cursor.execute('SELECT item FROM stocks')

    data = []

    for row in cursor.fetchall():
        data.append(row[0])

    return data

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


来源:https://stackoverflow.com/questions/41880447/i-want-to-populate-a-ttk-combobox-with-results-from-database-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!