0

i have 10 button, which correspond to the same method. how am i going to check which button was clicked in the corresponding method? i tried to check for the button press of a particular button in the list by the following code, but i got segmentation fault error:

for i in range(0,10):
    if button_list[i].clicked():
        break
 break
#operation with respect to the button clicked
1
  • post your event handler. the first argument in all GtkWidget event handlers should be a handle to the widget issuing the event. Commented Jan 10, 2012 at 19:32

2 Answers 2

3

Here's a sample code that illustrates knowing what button triggered the event by using the label of the button:

from gi.repository import Gtk

class ButtonWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Button Demo")
        self.set_border_width(10)

        hbox = Gtk.Box(spacing=6)
        self.add(hbox)

        #Lets create 10 buttons for this demo
        #You could create and set the label for 
        #each of the buttons one by one
        #but in this case we just create 10 
        #and call them Button0 to Button9
        for i in range(10):

            name = "Button{}".format(i)
            button = Gtk.Button(name)
            button.connect("clicked", self.on_button_clicked)
            hbox.pack_start(button, True, True, 0)


    def on_button_clicked(self, button):
        print button.get_label()

    def on_close_clicked(self, button):
        print "Closing application"
        Gtk.main_quit()

win = ButtonWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

So you could just check what the label is and act accordingly.

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

3 Comments

Welcome on SO. While it is a good practice to paste code in your answers as you did, it is very important to provide reference to the original thread. First, it shows respect to the author who wrote it. Furthermore, people can then follow the link in case there is more information. :) I suggest that you have a look to our FAQ : stackoverflow.com/faq
Thanks. The code pasted is really mine. The link to the gtk3 docs just links to an example of how to use buttons with python in gtk3. Cheers!
Oh Great, sorry about that then. I already edited your post to add the link to the docs. You could edit it again to specify that it point to gtk3 docs. You can achieve that by clicking on the "edit" link and you can always edit your own post. With good answer like yours, you are going to earn reputation fast and you will see that you can do a lot of things here with that. That's another reason why I point you to the FAQ ;) Have a nice day - Regards.
0

Once you have connected all the buttons to the same callback, I assume the callback will have this signature: callback(button) where button is the button that emitted the clicked signal.

Inside that callback should be easy to check which button was clicked using something like:

button_list.index(button)

This will return the index of the button inside your list.

4 Comments

i did not get the usage of 'button'. did not exactly get what it is
@GauravSood An argument that is present in every gtk callback is the widget that emitted the signal. For the clicked signal, that would be the button that was clicked. Hence your callback will always get a reference to the button that was clicked that can be used to check where in the button_list is located.
ok. so i have to retrieve the index of the button pressed as in the following code: index=button_list.index()
rather, i did not get what argument to give in the index method. i want to find out the index of the button in the list which was pressed

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.