2013-03-02 166 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'>