代码之家  ›  专栏  ›  技术社区  ›  Marco Scannadinari

编辑GtkWidget属性/属性

  •  3
  • Marco Scannadinari  · 技术社区  · 12 年前

    在大多数pygtk小部件页面中,它们包含名为“属性”、“属性”和“样式属性”的部分。如何更改这些属性和属性?

    4 回复  |  直到 12 年前
        1
  •  4
  •   ptomato    12 年前

    有三种方法可以更改属性:

    1. 正如zheoffec的回答一样,使用 set_property() 功能(或 set_style_property() 用于样式属性。)这个函数在Python中实际上不是必需的,但它是为了完整性,因为它是C API的一部分。

    2. 使用 props 属性您在文档中找到的任何属性都可以通过此属性进行访问。例如 btn1.props.label = 'StackOverflow' btn1.props.use_underline = False .

    3. 按照frb的建议使用getter和setter函数。它们之所以存在也是因为它们是C API的一部分,但有些人更喜欢它们而不是 支柱 属性此外,不能保证任何特定的属性都会有getter和setter函数!通常在设计良好的C API中,它们会出现在那里,但这不是必需的。

    对于样式属性,我认为唯一的选择是#1。对于“属性”,这些只是Python属性。要访问 allocation 属性,使用 btn1.allocation .

        2
  •  2
  •   user285594 user285594    12 年前

    要获取那里的所有小部件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'>
    
        3
  •  1
  •   Fredrick Brennan    12 年前

    在PyGTK中, GtkWidget 是所有其他小部件类(包括您自己创建的小部件类)从中继承的基类。

    就设置属性而言,您可能注意到无法直接设置它们:

    btn1.label = "StackOverflow"
    

    在PyGTK中,您需要在属性的名称前面加上 set_ ,如下所示:

    btn1.set_label("StackOverflow")
    

    如果有 - 在属性名称中,类似于 use-underline ,将其转换为下划线,如 set_use_underline 。我想说的是,我不认为这种使用getter和setter的做法是非常愚蠢的。

    这是一个完整的工作程序,取自 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()
    
        4
  •  1
  •   Marco Scannadinari    12 年前

    您可以使用 Gtk.Widget.set_property(property, value) 方法 property 应该是一个字符串。