0

I recently saw a question on "how to make a grid of buttons whose size changes automatically". I found a very interesting example of code there, however, the proposed method created the buttons in a "for", which did not allow them to be set to a specific parameter. Here is this code:

frame = Frame(root)
Grid.rowconfigure(root, 0, weight = 1)
Grid.columnconfigure(root, 0, weight = 1)
frame.grid(row = 0, column = 0, sticky = N + S + E + W)
grid = Frame(frame)
grid.grid(sticky = N + S + E + W, column = 0, row = 7, columnspan = 2)
Grid.rowconfigure(frame, 7, weight = 1)
Grid.columnconfigure(frame, 0, weight = 1)

for x in range(10):
    for y in range(5):
        btn = Button(frame)
        btn.grid(column = x, row = y, sticky = N + S + E + W)

for x in range(10):
    Grid.columnconfigure(frame, x, weight = 1)

for y in range(5):
    Grid.rowconfigure(frame, y, weight = 1)

Could you tell me how to make each button different?

Bethoth
  • 11
  • 7

1 Answers1

1

One issue I see here is you import tkinter as tk but do not use the prefix tk. when trying to set up your frames or buttons. This leads me to believe you may also be doing from tkinter import * and this is really bad idea especially when you write grid = Frame(root) as you are overwriting the grid() method one line before you actually try to use grid().

By using a list of buttons we can reference the index where the button is stored and do something with it.

Se below example and let me know if you have any questions:

import tkinter as tk


def some_function(ndex):
    print(button_list[ndex]['text'])
    button_list[ndex].config(text='', background='black')
    print(button_list[ndex]['text'])


root = tk.Tk()
root.geometry('300x200')
button_list = []

for x in range(15):
    root.columnconfigure(x, weight=1)
    for y in range(17):
        button_list.append(tk.Button(root))
        count = len(button_list)
        button_list[-1].config(text='{}'.format(count), command=lambda ndex=count-1: some_function(ndex))
        button_list[-1].grid(column=x, row=y, sticky='nsew')
        if x == 0:
            root.rowconfigure(y, weight=1)


root.mainloop()

For the fun of it and the approaching holiday here is a Jack-o'-lantern made from the code :D

enter image description here

To answer you question in the comments see below code:

import tkinter as tk


def some_function(value):
    print(value)


root = tk.Tk()
button_values = [['A', 'B', 'C'], ['=', '+', '-']]
button_list = []

for ndex, sub_list in enumerate(button_values):
    root.columnconfigure(ndex, weight=1)
    for sub_ndex, value in enumerate(sub_list):
        button_list.append(tk.Button(root))
        count = len(button_list)
        button_list[-1].config(text=value, command=lambda x=value: some_function(x))
        button_list[-1].grid(column=ndex, row=sub_ndex, sticky='nsew')


root.mainloop()

Results:

enter image description here

Console after pressing each button:

A
B
C
=
+
-
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • Hello. Thanks for the answer. What if I want to set a different text or command to each button, precisely? For example, I'd like to make the button in down right corner being a "=" which would send the result when I click on it... – Bethoth Oct 16 '19 at 12:40
  • That is easy. We just make a list of all the buttons you want and then use that list and index numbers to make your buttons. – Mike - SMT Oct 16 '19 at 12:45
  • @Bethoth I have added an example to my answer. – Mike - SMT Oct 16 '19 at 12:51
  • The lone thing that I still don't understand is how to give each button a different command... – Bethoth Oct 16 '19 at 12:59
  • Well if you want to do it dynamically you simply reference the value in a list that is related to your needs. Like if you are making a calculator. To accomplish this you need to use a nameless function called a lmabda. This allows you to write one line functions that can be used as a command. Here is an example where they do that to make a type of on screen keyboard. https://stackoverflow.com/questions/53647343/how-to-change-which-tkinter-button-is-highlighted-via-arrow-keys. – Mike - SMT Oct 16 '19 at 13:11