[pygtk] get_active() of CheckButton added to ScrolledWindow

Daniel Roesler diafygi at gmail.com
Thu Jan 22 07:40:36 WST 2009


Thanks Walter!

My example program now works with the "self.button" solution. Also, I
changed the self.window to self.gui to make the name more appropriate.

Revised program (working):
------------------------
import pygtk
import gtk
import gtk.glade

class app_gui:
 def __init__(self):
   self.gui = gtk.glade.XML("check.glade")
   dic = { "on_button1_clicked" : self.button1_clicked,
           "on_button2_clicked" : self.button2_clicked,
           "on_window1_destroy" : (gtk.main_quit) }
   self.gui.signal_autoconnect(dic)
   return

 def button1_clicked(self,widget):
   self.button = gtk.CheckButton("test")
   self.gui.get_widget("scrolledwindow1").add_with_viewport(self.button)
   self.button.show()
   return

 def button2_clicked(self,widget):
   status = self.button.get_active()
   print status
   return

app=app_gui()
gtk.main()
------------------------

Avast!
Daniel Roesler
diafygi at gmail.com

On Wed, Jan 21, 2009 at 1:34 AM, Walter Leibbrandt
<walter at translate.org.za> wrote:
> Hi,
>
> Firstly, on a conceptual note, "self.window" is not really an accurate name
> for the glade.XML object created. It does not represent any one window, but
> rather the whole GUI (windows and widgets) created in Glade. You can see the
> glade.XML object as a factory object, taking the XML output from the Glade
> UI designer ("check.glade" in your case) as input and giving you the
> constructed widgets as output.
>
> Having said that, there is no way for the glade.XML object to know about
> widgets that were created at run-time and not specified in its input XML and
> that is why self.window.get_widget("test") returns None. Also the string you
> passed as argument to gtk.CheckButton() is the button's label and not it's
> name. The difference is that the label is the string that is displayed on
> the button and the name is used as an internal string identifier for the
> widget.
>
> The most common way to achieve what you are trying to do here, is to keep a
> reference to all new, custom-created widgets. So if you replace "button =
> gtk.CheckButton('test')" with "self.button = gtk.CheckButton('test')", you
> can simply do "state = self.button.get_active()" in your "button2_clicked()"
> event handler.
>
> HTH
>
> Walter
>
> Daniel Roesler wrote:
>>
>> Howdy all,
>>
>> I'm just learning pygtk and glade, and I have a question about
>> accessing widgets that were added during an called function. In my
>> example, I have two buttons. The first button that adds a checkbox to
>> a scrolled window area. The second button prints the activity status
>> (True/False) of the checkbox.
>>
>> The first button works fine. However, when I push button two, python
>> says "AttributeError: 'NoneType' object has no attribute
>> 'get_active'". Obviously, this means that I'm not reaching the
>> checkbox widget correctly.
>>
>> So my question, how can I get properties of a widget added after the
>> initial glade file was loaded?
>>
>> Here's my program, and I've attached my glade file. Thanks all!
>> -----------------------
>> import pygtk
>> import gtk
>> import gtk.glade
>>
>> class app_gui:
>>  def __init__(self):
>>    self.window = gtk.glade.XML("check.glade")
>>    dic = { "on_button1_clicked" : self.button1_clicked,
>>            "on_button2_clicked" : self.button2_clicked,
>>            "on_window1_destroy" : (gtk.main_quit) }
>>    self.window.signal_autoconnect(dic)
>>    return
>>
>>  def button1_clicked(self,widget):
>>    button = gtk.CheckButton("test")
>>    self.window.get_widget("scrolledwindow1").add_with_viewport(button)
>>    button.show()
>>    return
>>
>>  def button2_clicked(self,widget):
>>    status = self.window.get_widget("test").get_active()
>>    print status
>>    return
>>
>> app=app_gui()
>> gtk.main()
>> -----------------------
>>
>> Avast!
>> Daniel Roesler
>> diafygi at gmail.com
>>  ------------------------------------------------------------------------
>>
>> _______________________________________________
>> pygtk mailing list   pygtk at daa.com.au
>> http://www.daa.com.au/mailman/listinfo/pygtk
>> Read the PyGTK FAQ: http://faq.pygtk.org/
>
> --
> Walter Leibbrandt                  http://translate.org.za/blogs/walter
> Software Developer                                  +27 12 460 1095 (w)
> Translate.org.za
>
> Recent blogs:
> * Virtaal's MVCisation
> http://www.translate.org.za/blogs/walter/en/content/virtaals-mvcisation
> * Things that changed the way I code
> * Switching from Subversion to git
>
>
>


More information about the pygtk mailing list