2012-10-15 30 views
0

使用Glade快速編寫簡單的GUI,我試圖創建一個通用的錯誤對話框,我可以將標籤的文本設置爲任何錯誤。在典型的GUI開發中非常簡單(獲取子表單,設置標籤的標題屬性等)。在Glade/PyGTK對話窗口中設置標籤

我似乎無法弄清楚如何在PyGTK/Glade中控制該標籤。

這裏是我的對話框中的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> 

這裏是我想,有2次嘗試相關的Python代碼。第一次我試圖設置錯誤對話框的文本字段,第二次我添加了一個標籤,並嘗試先獲取並設置該標籤。

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 

回答

3

爲您提供您可以設置標籤的文本與

dError.label.set_text("test") 

您的問題XML是你訪問MessageDialog,而不是標籤本身。以上是一條捷徑,更一般地可以訪問標記(S)(應該很容易與您的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 
+0

謝謝!像魅力一樣工作 – espais