Home > Software engineering >  Trying to create rows of buttons in tkinter for items in a list
Trying to create rows of buttons in tkinter for items in a list

Time:12-05

I want to create buttons for every item in a list, but need to find out how to fit them all in the same frame. Ideally I would like to put 4 or 5 buttons in a row, then the next 4-5 buttons are displayed directly under. Here is what I have now:

from tkinter import ttk
from restaurantSoftware import restaurantInfo


class menuFrame:
    def __init__(self, frame):
        self.frame = frame

        availableItems = restaurantInfo.readRestaurantInfo('availableItems.csv')
        itemList = availableItems[0]
        priceList = availableItems[1]
        categoryList = availableItems[2]

        for item in itemList:
            button = ttk.Button(text=item)
            button.pack(side='left', ipadx=25, ipady=25)

Here is a screenshot of what my tkinter layout looks like when I run the program.

layout preview

CodePudding user response:

You can use the built-in divmod function which returns the multiplier and remainder such that a, b = divmod(c, d) => a * d b = c which allows to calculate the row and column where the widget should be placed:

import tkinter as tk


COLUMNS = 3

item_list = ['Cheese burger', 'BLT', 'Cheese Pizza', 'Chicken Fillet',
             'Hot dog', 'Caesar Salad', 'Chicken Salad']

root = tk.Tk()

for i, item in enumerate(item_list):
    lbl = tk.Button(root, text=item)
    row, column = divmod(i, COLUMNS)
    lbl.grid(row=row, column=column, sticky='news')

root.mainloop()

CodePudding user response:

The easiest way would be to use grid instead of pack and a for-loop to place the buttons on the grid. Here's an example of how that may be done:

import tkinter as tk

root = tk.Tk()

item_list = ['Cheesburger','BLT','Cheese Pizza','Chicken Filet',
             'Hotdog','Caesar Salad','Chicken Salad']
row_length = 4

for row in range(1000):  # Any number bigger than anticipated number of rows
    if row_length*row >= len(item_list):
        break
    for col in range(row_length):
        if row_length*row   col >= len(item_list):
            break
        b = tk.Button(root, width=15, text=item_list[row_length*row   col])
        b.grid(row=row, column=col)

root.mainloop()

Since I will be breaking out of the loops it doesn't really matter how big the range in the first loop is. Now this solution may definitively be improved upon, but it's a start.

  • Related