1

I'm making a project which requires me to make 270 buttons in tkinter, I'm creating them via a loop and i need to add functionality to them. In order for them to be functional for my purpose I need to find out which button is being pressed how do i do this?

heres my code (dont worry about the loads of comments, its necessary as its for a project for my exams)

Code

The blue part is where the buttons are created. Thanks

I've really not tried anything, I've tried to get the text of the button's as this will be actually what I want. The buttons are named after all the London Underground stations and so getting the text of the button will be sufficient. I just dont rlly know what I'm doing

BigJ
  • 23
  • 4
  • 2
    Could you please add the relevant code directly in your post as a formatted code block instead of an image? It help for searches and for writing answers – nigh_anxiety Apr 14 '23 at 21:26

1 Answers1

1

It seems the CTkButton object has an parameter called command to which you can pass a callback function (see here).

In this case, using a lambda function should be the best approach.

for line in stations_reader:
    ct.CTkButton(frame, text=line[0], font=(Ariel, 15), command=lambda: line[0]).grid(row=(int(line[5])+1),column=0)
    ct.CTkButton(frame, text=line[0], font=(Ariel, 15), command=lambda: line[0]).grid(row=(int(line[5])+1),column=0)

I think this should roughly get you the behaviour you want, based on your question, but make sure to comment if you have any questions.

Cmd858
  • 761
  • 1
  • 6
  • 10