Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tkinter formatting rows

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’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading