0

I am building a restaurant menu using tkinter, python and sql tables, and I want the label widget "list" to display the menu items. So far, I have these functions (not the whole program):

`list = tk.Label(text = '',bg = 'black',font = ('Baskerville', 20))
list.grid(column = 0,row = 2, columnspan = 4,sticky = 'W')

#Setup - Python
descEntree = {
    1:"Flour tortilla triangles filled with shredded cheddar, monterey jack, chicken, salsa and pico de gallo",
    2:"Mini sandwiches with salmon, cream cheese, chives and gouda"
}
descMain = {}
descDessert = {}

def getItem(ID, course):
    currentItem = ''
    table_name = "Menu" + course
    query = "SELECT Item FROM {} WHERE ID = %s".format(table_name)
    cur.execute(query, (ID,))
    result = cur.fetchone()
    currentItem = result[0]
    return currentItem

def printItems(course):
    cur.execute("select max(ID) from {}".format("Menu" + course))
    n = cur.fetchone()
    for i in range(1,n[0] + 1):
        list.config(text = '\n' + getItem(i,'Entree') + '\n' + descEntree[i])

printItems('Entree')

w.mainloop()`

However it only prints the last item and desc in the table:

https://i.stack.imgur.com/bWqCO.png

  • Labels aren't designed to be used like a print function. While it can display multiple lines, it's not really optimal for that. For example, you can't scroll a label. Maybe you should consider using a `Text` widget instead. It's designed to display multiline text. – Bryan Oakley Jul 02 '23 at 05:53
  • In fact, you can actually use the `print` function itself to send text to a text widget. See [How to redirect print statements to Tkinter text widget](https://stackoverflow.com/questions/12351786/how-to-redirect-print-statements-to-tkinter-text-widget) – Bryan Oakley Jul 02 '23 at 06:27

1 Answers1

0

I think this is caused because Label.config(text="<new_text>") replaces all current text in the label. so if you call Label.config(text="<new_text>") 2 times, only the last "<new_text>" will be displayed. to add text to a label, you can use Label.config(Label.cget("text") + "<text_to_add>")

modified python code:

list = tk.Label(text = '',bg = 'black',font = ('Baskerville', 20))
list.grid(column = 0,row = 2, columnspan = 4,sticky = 'W')

#Setup - Python
descEntree = {
    1:"Flour tortilla triangles filled with shredded cheddar, monterey jack, chicken, salsa and pico de gallo",
    2:"Mini sandwiches with salmon, cream cheese, chives and gouda"
}
descMain = {}
descDessert = {}

def getItem(ID, course):
    currentItem = ''
    table_name = "Menu" + course
    query = "SELECT Item FROM {} WHERE ID = %s".format(table_name)
    cur.execute(query, (ID,))
    result = cur.fetchone()
    currentItem = result[0]
    return currentItem

def printItems(course):
    cur.execute("select max(ID) from {}".format("Menu" + course))
    n = cur.fetchone()
    for i in range(1,n[0] + 1):
        list.config(text = list.cget("text") + ('\n' + getItem(i,'Entree') + '\n' + descEntree[i]))

printItems('Entree')

w.mainloop()