views:

38

answers:

1

Environment:

  • Built interface using Glade3.
  • Backend is written in Python using the GTK+ Builder library.

-

Although I know the method I need to use to update a label's text (label.set_text("string")), I'm having trouble obtaining the label object in the python code.

Here's what my code looks like:

#!/usr/bin/python
# Filename: HelloPython.py
# Author: Andrew Hefley Carpenter
# Date: 18 August 2010

import sys
import gtk

class HelloPython:

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

    def __init__(self):

        builder = gtk.Builder()
        builder.add_from_file("HelloPython.xml") 

        self.window = builder.get_object("window")
        builder.connect_signals(self)   

    def on_button1_clicked(self, widget):

        print "Hello World!"  
        widget.set_label("Hello World!")
        #I'd like to update 

if __name__ == "__main__":
    editor = HelloPython()
    editor.window.show()
    gtk.main()

End goal: I want to update "Object X" using it's set_text method after the callback to "Object Y" (in this case button1) as handled by "on_button1_clicked"

A: 

The widget parameter to on_button1_clicked is a gtk.Button, not a gtk.Label. gtk.Button has a convenience api method called set_label().

This only works if the child of Gtk.Button is a gtk.Label. This is the default when creating a new button in Glade-3, but if you've changed the contents of the button, this will not work and you'll need a reference to the gtk.Label widget itself.

EDIT (code to update the label):

class HelloPython:
    def on_window_destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        builder = gtk.Builder()
        builder.add_from_file("HelloPython.xml") 
        self.window = builder.get_object("window")
        self.label = builder.get_object("label1") # get reference to the label
        builder.connect_signals(self)   

    def on_button1_clicked(self, widget):
        #widget.set_label("Hello World!") this would set the button's text
        self.label.set_text("Hello World!") # this sets the label's text
anthony
Understandable. The container hierarchy is as follows:-Window has one child, vertical box '2 containers'-vertical box has two children, button1 and label1. -I'd like to have label1 update when it's sibling, button1 is clicked. How can I achieve this/what conceptual barriers are preventing me from figuring this out?Thanks for the quick response.
Drew
ah, gtk doesn't know or care about this "sibling" relationship. you'll need to get a reference to the label using gtk.Builder.get_object() and call set_text on that object.
anthony
also, you'll want to get out of the habit of using glade's default widget names (button1, label1) as soon as possible, otherwise your life will suck.
anthony
OH NO I'VE BEEN -1ed BY KILLOWN!
anthony
Thank you very much. This is the very beginning of a project that will help the bioinformatics community by visualizing splice junctions.
Drew