0

I am developing a small application using Gtk-Python, in which i have some 10 to 20 buttons, and when any of them are clicked, i want the value of the button to be appended in the text box I have done it so far by

button1.connect("clicked",button_function)
button2.connect("clicked",button_function)
...

for all the functions. Is this the only way of doing it? Or is there an elegant way? And the value of the button is specified by

button1._value=#something
...

can anyone help me?

4
  • 1
    in this way, pass the button objects in a list, call it like this, for button in buttonslist: button.connect("clicked",button_function) Commented May 10, 2013 at 5:22
  • but won't that call the functions for all the buttons at once? Commented May 10, 2013 at 6:12
  • You are just binding the function to click event,you can call whenever you want on any button any time. Commented May 10, 2013 at 6:27
  • @Srini Could you post your comment as a response so that Aswin Murugesh can accept it? Commented May 11, 2013 at 10:49

2 Answers 2

1

In this way, put button objects in a list, call it like this,

for button in buttonslist: 
    button.connect("clicked",button_function)
Sign up to request clarification or add additional context in comments.

Comments

0
import sys,os
import pygtk, gtk, gobject

class GTK_Main:
  def __init__(self):


    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_title("Test")
    window.set_default_size(500, 400)
    window.connect("destroy", gtk.main_quit, "WM destroy")

    hbox_eq = gtk.HBox()

    buttonlist = []

    for i in [1,2,3,4,5,6,7,8,9]:
      b =  gtk.Button( str(i) )
      b.set_name("YOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!")

      buttonlist.append(b)
      hbox_eq.pack_start(b, True, True, 0)

    for button in buttonlist:
      print button
      #print button.get_label()
      #print button.get_name()

    window.add(hbox_eq)
    window.show_all()

  def exit(self, widget, data=None):
    gtk.main_quit()


GTK_Main()
gtk.gdk.threads_init()
gtk.main()

output:

<gtk.Button object at 0x25d8370 (GtkButton at 0x24348e0)>
<gtk.Button object at 0x25d83c0 (GtkButton at 0x24349a0)>
<gtk.Button object at 0x25d8410 (GtkButton at 0x2434a60)>
<gtk.Button object at 0x25d8460 (GtkButton at 0x2434b20)>
<gtk.Button object at 0x25d84b0 (GtkButton at 0x2434be0)>
<gtk.Button object at 0x25d8500 (GtkButton at 0x2434ca0)>
<gtk.Button object at 0x25d8550 (GtkButton at 0x2434d60)>
<gtk.Button object at 0x25d85a0 (GtkButton at 0x2434e20)>
<gtk.Button object at 0x25d85f0 (GtkButton at 0x2434ee0)>

Comments

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.