Python Forum
Populating a Listbox from db Query
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Populating a Listbox from db Query
#1
Good morning, can someone point me to literature or offer advice to help me in my current issue.
I am trying to insert the data queried from my database into a Listbox. I have been doing this successful but I am stuck on how to insert the result of the query into the Listbox easily without multiple "insert" lines as showing below.

I currently have the following working:
    def select():
        try:
            selection = var.get()
            c.execute('SELECT Recipe FROM Recipes WHERE Dish_Type = :selection', {"selection": selection})
            results = c.fetchall()
            output0.delete(0, END)
            output0.insert(0,results[0])
            output0.insert(0,results[1])
            output0.insert(0,results[2])
            output0.insert(0,results[3])
            output0.insert(0,results[4])
            output0.insert(0,results[5])
            output0.insert(0,results[6])
            output0.insert(0,results[7])
            output0.insert(0,results[8])
            output0.insert(0,results[9])
#================================= OptionMenu ==================================
    var = tkinter.StringVar(window_2)
    var.set('Menu')
    choices = [
        'Appetizers',
        'Beef',
        'Bread',
        'Cake',
        'Chicken',
        'Chilli',
        'Curry',
        'Desert',
        'Drinks',
        'Egg',
        'Fish',
        'Pasta',
        'Pork',
        'Potato',
        'Rice',
        'Salad',
        'Sandwich',
        'Sauce',
        'Sea Food',
        'Slow Cooker',
        'Soup',
        'Stew',
        'Tofu',
        'Vegetables']
    option = tkinter.OptionMenu(window_2, var, *choices)
    option.config(font=('Times 9 bold'), bg = "#F9F8D6", fg = "#9A0615")
    option.place(x=215, y=120)
    option["menu"].config(bg = "#F9F8D6", fg = "#9A0615")
    button = tkinter.Button(window_2, text="Load Category", font=('Times 9 bold'), bg = "#F9F8D6", fg = "#9A0615", command=select)
    button.pack
    button.place(x=363, y=120)
#================================ OUTPUT PANE 1 ================================
    output0 = tkinter.Listbox(window_2, font=('Times 9'), height = 20, width=40, bd=0, bg = "#FFD599", fg = '#9A0615')
    output0.pack()
    output0.place(x=210, y=190)
Is there a wildcard value that can be used or a better way of writing the "insert" instead of using the multiple "insert(0,results [0])" lines?
This way works and populates the Listbox but it does it in reverse order of the rows (last to first), in the table and it also encases the first entries (row 1 & 2), in {} brackets for some reason.
I would like to find the proper way to have the query display in proper order first - last( or if it possible in tkinter to have it query and output in alphabetical order would be nice, if that is possible), and also without the {} brackets.

Also when I query the database and currently if there is only 3 entries in the table the interpreter will show
"output0.insert(0,results[3])
IndexError: list index out of range"
because of the additional insert lines which there is no data to be pulled in the table yet.
Looking for advice on these issues.

Thanks in advance.
"Often stumped... But never defeated."
Reply
#2
Use a for
for each_result in results:
    output0.insert(0,each_result)
Reply
#3
This was just the resolve I have been trying to achieve.

I will work on the {} bracket issue next and then onto trying to code the query to pull the data from the table in alphabetical order.

Thank you, I appreciate the assistance.
"Often stumped... But never defeated."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] populating dropdown from Method mikisDW 2 3,814 Apr-06-2020, 08:06 PM
Last Post: mikisDW
  PyQt, Open a Table when a row is selected populating it with the row values rarevesselt 18 15,163 Mar-30-2019, 12:57 AM
Last Post: rarevesselt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020