1

I'm new to using tkinter and GUI programming in general so this may be a pretty basic question. Anyway, suppose I have a set of buttons for the user to choose from and what I want is a list of all the button objects that were clicked by the user. Basically, I want to know which buttons the user clicked.

3 Answers3

1

Here's a good website covering Python events that should guide you:

http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

It sounded like you might want something akin to a checkbox, if so:

Cannot understand how to get data from checklistbox in wxpython

Community
  • 1
  • 1
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
  • 1
    I read through the events and bindings page and I was thinking something like this could work: if myButton.bind(""): return the button label (if there is such a method). But it doesn't seem to do anything. Any thoughts? –  May 31 '11 at 20:28
1

On each of the buttons you could set the command to add themselves to a list of clicked buttons.

clicked = []
foo = Button(root, text='bar', command=lambda self:clicked.append(self))

Not sure if the syntax is all correct, but that's the basic idea.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
  • I realized that what I actually wanted was the text of the button so I tried to use the lambda function like this: button1.config(command=(lambda x: (clicked.append(x)))(button1.cget("text"))). It seems to make sense to me but for some reason it returns the list immediately i.e. doesn't wait for the user to click. Any ideas on why this may be? –  Jun 01 '11 at 02:44
  • This would normally be if you had typed button1.config(command=callback()) rather than button1.config(command=callback). You might want to de-anonymise the function (i.e. make it an actual function rather than lambda) for readability, that'd help you debug. – TartanLlama Jun 01 '11 at 13:31
1

Here is a simple example of what you could do to find out if a button has or has not been pressed yet.

import tkinter.ttk, functools

class Example(tkinter.Tk):

    def __init__(self, buttons):
        super().__init__()
        self.button_set = set()
        for button in range(buttons):
            button = tkinter.ttk.Button(self, text='Button '+str(button))
            button.pack()
            self.button_set.add(button)
        self.setup_buttons()
        self.bind('<Escape>', self.check_buttons)
        self.mainloop()

    def setup_buttons(self):
        for button in self.button_set:
            button['command'] = \
                functools.partial(setattr, button, 'pressed', True)
            button.pressed = False

    def check_buttons(self, event):
        for button in self.button_set:
            print('Button {} has{} been pressed.'.format(id(button),
                (' not', '')[button.pressed]))

if __name__ == '__main__':
    Example(5)

When the code is run, you may press the Escape key to get a printout in the console of what buttons have been pressed. Pressing the buttons will set their pressed attribute to true, and you can get another printout of what buttons have been pressed. Programatically, you would follow the example in the check_buttons method to find out if a button has been pressed or not.

Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117