简体   繁体   English

在Glade / PyGTK对话框窗口中设置标签

[英]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. 使用Glade快速编写简单的GUI的代码,我试图创建一个通用的错误对话框,在其中可以将标签的文本设置为任何错误。 Pretty straightforward in typical GUI development (get the child form, set label's caption attribute, etc.). 在典型的GUI开发中非常简单(获取子窗体,设置标签的标题属性等)。

I can't seem to figure out how to get control of that label in PyGTK/Glade though. 我似乎无法弄清楚如何在PyGTK / Glade中控制该标签。

Here is the XML for my dialog... 这是我对话框的XML ...

<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. 这是我尝试的相关Python代码,尝试了2次。 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

For the xml you supplied you can set the label's text with 对于您提供的xml,您可以使用

dError.label.set_text("test")

Your problem was that you were accessing the MessageDialog and not the label itself. 您的问题是您正在访问MessageDialog,而不是标签本身。 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): 上面是一个捷径,更一般而言,您可以访问标签(应该通过与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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM