2013-03-02 117 views

回答

4

有三種方法來更改屬性:(樣式屬性或set_style_property()

  1. 正如zheoffec的回答,請使用set_property()功能,此功能其實並沒有在Python必要的,但它是有完整性,因爲它是C API的一部分。使用props屬性。您可以通過此屬性訪問您在文檔中找到的任何屬性。例如,btn1.props.label = 'StackOverflow'btn1.props.use_underline = False

  2. 按照frb的建議使用getter和setter函數。這些也僅存在,因爲它們是C API的一部分,但有些人更喜歡它們的props屬性。此外,不保證任何特定的財產將具有吸氣和設置功能!通常在精心設計的C API中,它們將會在那裏,但它不是必需的。

對於樣式屬性,我相信唯一的選擇是#1。對於「屬性」,這些只是Python屬性。要訪問allocation屬性,請使用btn1.allocation

+0

很好的答案,謝謝 – 2013-03-03 10:48:26

1

在PyGTK中,GtkWidget是所有其他窗口小部件類(包括您自己製作的窗口類)繼承的基類。

至於設置屬性的話,你可能會注意到你不能直接將它們設置:

btn1.label = "StackOverflow" 

在PyGTK的,你需要set_的前綴屬性的名稱,就像這樣:

btn1.set_label("StackOverflow") 

如果在屬性名稱中有-,就像use-underline一樣,將它們變成下劃線,如set_use_underline。我想說,我不認爲這種使用getter和setter的方法是非常pythonic。

這是一個完整的工作程序,取自ZetCode tutorial並進行了修改。

import gtk 

class PyApp(gtk.Window): 
    def __init__(self): 
     super(PyApp, self).__init__() 

     self.set_title("Buttons") 
     self.set_size_request(250, 200) 
     self.set_position(gtk.WIN_POS_CENTER) 

     btn1 = gtk.Button("Button") 
     btn1.set_label("StackOverflow") 
     btn1.set_use_underline(False) 

     fixed = gtk.Fixed() 

     fixed.put(btn1, 20, 30) 

     self.connect("destroy", gtk.main_quit) 

     self.add(fixed) 
     self.show_all() 


PyApp() 
gtk.main() 
+0

是的,我知道這一點,而是採取例如pygobject的Gtk.RadioToolButton,不具有一個獨立的'join_group()'方法,除了創建一個新的'RadioToolButton'並將其添加到一個組中的方法。如果我想修改現有的RadioToolButton組,我需要修改一個屬性(我剛剛發現了該怎麼做;)(請參閱我的答案))。 – 2013-03-02 23:32:44

1

您可以使用Gtk.Widget.set_property(property, value)方法更改Widget屬性。 property應該是一個字符串。

+1

它實際上是一個GObject方法,它可以處理不是小部件的對象,例如gtk.TextTag。 – Dave 2013-03-16 20:42:33

1

要獲取所有控件有widget.pros列表:

button = gtk.Button() 
for pspec in button3.props: 
    print pspec 
    #print button3.get_property(pspec.name) 

輸出:

<GParamObject 'related-action'> 
<GParamBoolean 'use-action-appearance'> 
<GParamPointer 'user-data'> 
<GParamString 'name'> 
<GParamObject 'parent'> 
<GParamInt 'width-request'> 
<GParamInt 'height-request'> 
<GParamBoolean 'visible'> 
<GParamBoolean 'sensitive'> 
<GParamBoolean 'app-paintable'> 
<GParamBoolean 'can-focus'> 
<GParamBoolean 'has-focus'> 
<GParamBoolean 'is-focus'> 
<GParamBoolean 'can-default'> 
<GParamBoolean 'has-default'> 
<GParamBoolean 'receives-default'> 
<GParamBoolean 'composite-child'> 
<GParamObject 'style'> 
<GParamFlags 'events'> 
<GParamEnum 'extension-events'> 
<GParamBoolean 'no-show-all'> 
<GParamBoolean 'has-tooltip'> 
<GParamString 'tooltip-markup'> 
<GParamString 'tooltip-text'> 
<GParamObject 'window'> 
<GParamBoolean 'double-buffered'> 
<GParamUInt 'border-width'> 
<GParamEnum 'resize-mode'> 
<GParamObject 'child'> 
<GParamString 'label'> 
<GParamObject 'image'> 
<GParamEnum 'relief'> 
<GParamBoolean 'use-underline'> 
<GParamBoolean 'use-stock'> 
<GParamBoolean 'focus-on-click'> 
<GParamFloat 'xalign'> 
<GParamFloat 'yalign'> 
<GParamEnum 'image-position'>