Home > other >  Tkinter formatting rows
Tkinter formatting rows

Time:07-29

I am using Tkinter and have a grid that is successfully outputting the first 2 rows as intended. Now, every time I run the program I want to display a variable number of rows (always after those first 2 rows) starting at row = 2, column = 0. So if there are 4 items to display, all would be shown on the left side one under the other.

Currently, the program only works if there is only 1 item to display. Then it will output properly. If there are more products, then only the last in the list will display.

I tried to do something like .grid(row=2 product), but then I get an error - TypeError: unsupported operand type(s) for : 'int' and 'dict'.

How can I handle using a variable number of products to create a variable number of labels and display all of them properly?

from tkinter import * 
window=Tk()
for product in productList:
    productLabel = Label(
        window,
        bg='#DC143C'
    ).grid(row=2, column=0)
window.mainloop()

CodePudding user response:

You can use enumerate() function to get a index and add it to the row option:

for i, product in enumerate(productList):
    productLabel = Label(
        window,
        text=product,
        bg='#DC143C'
    ).grid(row=2 i, column=0)
  • Related