Here's how I would do the buttons:
If you try this, here's what you get:
You can even add the particular function you want to execute when the buttonis pressed (If you'd like to do this, I can show you how)
import tkinter as tk class BtnEngine: # rename class as you wish def __init__(self, parent): self.parent = parent self.parent.title("Tryout7") self.parent.minsize(100, 100) self.btn_row = 0 self.btn_column = 0 self.number_of_btns = 12 self.btns_per_row = 4 self.btn_number = 0 self.btns = [None] * self.number_of_btns self.dispatch() def dispatch(self): self.create_buttons() def create_buttons(self): btn_titles = ["volume up", "volume down", "channel up", "channel down", "conditioner on", "conditioner off", "raise the bed ", "lower the bed", "open television", "mute", "open curtain", "close curtain"] while self.btn_number < self.number_of_btns: if not ((self.btn_column + 1) % self.btns_per_row): self.btn_row += 1 self.btn_column = 0 self.btns[self.btn_number] = (tk.Button(self.parent, text=btn_titles[self.btn_number], padx=60, pady=20, height = 2, width = 5, command=lambda idx = self.btn_number, title = btn_titles[self.btn_number]: self.button_clicked(idx, title))) self.btns[self.btn_number].grid(row=self.btn_row, column=self.btn_column) self.btn_column += 1 self.btn_number += 1 def button_clicked(self, idx, title): print(f"Button {idx+1} named {title} pressed") # add code for specific button here def main(): root = tk.Tk() BtnEngine(root) root.mainloop() if __name__ == '__main__': main()
If you try this, here's what you get:
Output:Button 1 named volume up pressed
Button 2 named volume down pressed
Button 5 named conditioner on pressed
Button 12 named close curtain pressed
You can replace the print statements with the functions that you want to perform when the particular button is pressed.You can even add the particular function you want to execute when the buttonis pressed (If you'd like to do this, I can show you how)