2

I have this program i'm trying to create, and basically i need to make 8 rows of buttons in tkinter but i can't figure out how i could do this with a loop , without a loop i did this :

def decimal():
app = Toplevel(root)
app.title("Traducteur décimal")
app.geometry("400x200")
r1 = Button(app, text="LED 1 ON")
r2 = Button(app, text="LED 1 OFF")
r1.place(x=125,y=0)
r2.place(x=225,y=0)
r3 = Button(app, text="LED 2 ON")
r4 = Button(app, text="LED 2 OFF")
r3.place(x=125, y=40)
r4.place(x=225, y=40)
r5 = Button(app, text="LED 3 ON")
r6 = Button(app, text="LED 3 OFF")
r5.place(x=125, y=80)
r6.place(x=225, y=80)

By the way i'm sorry for the bad English. Thanks

2
  • by the way i know this could be possible with object oriented programming but i have very little experience with it Commented Apr 10, 2021 at 8:40
  • Use grid(), you will get a better answer faster. Commented Apr 10, 2021 at 9:39

1 Answer 1

1

A way is to put them all in a list of 2-tuples with a for loop. Being a list, you can access its elments after via indexing:

buttons = []

x_loc_on, x_loc_off = (125, 225)

y_start = 0
y_offset = 40

commands = [<16 functions here>]

for row in range(8):
    # calculate the pair's y-position based on row
    y_pos_of_row = y_start + row * y_offset

    # get the row number (starts from 1 unlike the variable `row`; so adding 1)
    row_number = row + 1

    # generate the ON button
    button_1 = Button(app, text=f"LED {row_number} ON", command=commands[row])
    button_1.place(x=x_loc_on, y=y_pos_of_row)

    # generate the OFF button
    button_2 = Button(app, text=f"LED {row_number} OFF", command=commands[row+1])
    button_2.place(x=x_loc_off, y=y_pos_of_row)

    # put this row's ON-OFF button pair as a 2-tuple into a list
    buttons.append((button_1, button_2))

Then you can access ith row and ON button via buttons[i][0] and same row OFF button via buttons[i][1].

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, this does indeed works but how can i modify the text into it ? The buttons are only displaying LED 1 ON and LED 1 OFF
@Tahet Yes, sorry about that.. Now fixed with an f-string.
Im sorry it may sound stupid but how do i interact with these new buttons? I need to put command parameters in them
@Tahet No, not at all stupid but can you be more specific? Do you want one command for all of them, or one command per row, or one command for each button, or one command for ON buttons and one command for OFF buttons etc.
@Tahet I see; then you can form a list of 16 commands for all buttons. Then since row is going from 0 to 7, we can index that list with row and row+1 in each turn of for loop to get the corresponding command for each button. Edited the answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.