erik
erik

Reputation: 3880

Set label in Glade/PyGTK dialog window

Using Glade to quickly code up a simple GUI, and I'm trying to create a generic error dialog in which I can set a label's text to whatever the error is. Pretty straightforward in typical GUI development (get the child form, set label's caption attribute, etc.).

I can't seem to figure out how to get control of that label in PyGTK/Glade though.

Here is the XML for my dialog...

<object class="GtkMessageDialog" id="dError">
    <property name="can_focus">False</property>
    <property name="border_width">5</property>
    <property name="type_hint">dialog</property>
    <property name="skip_taskbar_hint">True</property>
    <property name="message_type">error</property>
    <property name="buttons">close</property>
    <child internal-child="vbox">
      <object class="GtkVBox" id="dialog-vbox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkHButtonBox" id="dialog-action_area">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="layout_style">end</property>
            <child>
              <placeholder/>
            </child>
            <child>
              <placeholder/>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="pack_type">end</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="lblError">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>

And here is the associated Python code I'm trying, with 2 attempts. The first I was trying to set the text field of the Error dialog, and the second I'd added a label and tried to first get and set that.

dError = self.builder_.get_object("dError") # get dialog

# Attempt 1 - setting the text field of the error dialog
# dError.set_text("Attempt 1")  
    #-- AttributeError: 'gtk.MessageDialog' object has no attribute 'set_text'

# Attempt 2 -setting an added label
# dLbl = dError.get_object("lblError")
    #-- AttributeError: 'gtk.MessageDialog' object has no attribute 'get_object'
# dlbl.set_text("Attempt 2")  

dError.show()
return True

Upvotes: 0

Views: 1760

Answers (1)

deinonychusaur
deinonychusaur

Reputation: 7304

For the xml you supplied you can set the label's text with

dError.label.set_text("test")

Your problem was that you were accessing the MessageDialog and not the label itself. The above is a short-cut, more generically you can access the label(s) (should be easy to trace how it works by comparing with your xml):

vbox = dError.get_child()
hbox, label1, hbuttonbox = vbox.get_children()
label1.set_text("Test1")
im, vbox2 = hbox.get_children()
label2, label3 = vbox2.get_children()
label2.set_text("Text2")
label3.set_text("Text3") #This one is invisible as default

Upvotes: 3

Related Questions