A9M
A9M

Reputation: 55

Tkinter Using loops to create buttons and writing to buttons from a dimensional list

I'm learning to use loops within tkinter to create buttons and write from a two dimensional list to buttons.

My first question is what I would like to happen is the bottom two buttons (PressBtns) are named button 1 and button 2 respectively, not sure how to do this when using loops.

My second query is when these buttons are pressed "Button 1" prints the first part of the two dimensional list onto (ShowBtns) (["10", "20", "40", "80", "90", "100"]) one number to each button and when (Button 2) is pressed it prints the second part of the two dimensional list onto the (Showbtns)(["95", "90", "80", "40", "20", "10"]]) overwriting the first list. Thanks for your help in advance see my code below.

import tkinter as tk

window = tk.Tk()

Lists=[["10", "20", "40",  "80", "90", "100"], 
["95", "90", "80", "40", "20", "10"]]

def List1():
    j= 6
    for row in (Lists):
        for col in row [0:j]:
            j=j-1
            PressBtns.config (text = col)
            

def List2():
    j= 6
    for row in (Lists):
        for col in row [1:j]:
            j=j-1
            PressBtns.config (text = col)
            

for i in range (6):
    ShowBtns = tk.Button (window, height = 1, width = 5)
    ShowBtns.grid (row = i, column = 5)

counter = 6
for i in range (len(Lists)):
    PressBtns = tk.Button (window, height = 1, width = 10, command = "")
    PressBtns.grid(row = counter, column = 5)
    counter = counter +1

window.mainloop()
        

Upvotes: 2

Views: 137

Answers (1)

Flavio Moraes
Flavio Moraes

Reputation: 1351

for your first question you can name the buttons with the option text. In your case you can simply do PressBtns = tk.Button (window, text="Button {}".format(i+1), height = 1, width = 10, command = "") inside the for loop.

The second question is a bit more complicated, first, if you want to change the buttons that you already created, they must be accessible, which means that they must be created in independent variables or in a list. So I suggest you to create the buttons like this:

ShowBtns = []
for i in range (6):
    ShowBtns.append(tk.Button (window, height = 1, width = 5))
    ShowBtns[-1].grid (row = i, column = 5)

Now we can create a function that receives a list and change the text of each button to the number in the list:

def change_btText(names):
    for i in range(len(ShowBtns)):
        ShowBtns[i]["text"] = names[i]
        window.update()

the last thing to do is to add this function as callback function of button 1 and 2 when you create it:

for i in range (len(Lists)):
    PressBtns = tk.Button (window, text="Button {}".format(i+1), height = 1, width = 10, command = lambda x=i: change_btText(Lists[x]))
    PressBtns.grid(row = counter, column = 5)
    counter = counter +1

see here that you cannot use Lists[i], you have to create a x=i otherwise both buttons will use the last value of i and not the value when the button is created.

Upvotes: 1

Related Questions