Editing GtkWidget attributes/properties

2019-04-12 11:37发布

问题:

In most pygtk widget pages, they contain sections called 'Attributes', 'Properties', and 'Style Properties'. How can I change these properties and attributes?

回答1:

There are three ways to change properties:

  1. As in zheoffec's answer, use the set_property() function (or set_style_property() for style properties.) This function is actually not necessary in Python, but it is there for completeness because it is part of the C API.

  2. Use the props attribute. Any property that you find in the documentation can be accessed through this attribute. For example, btn1.props.label = 'StackOverflow' and btn1.props.use_underline = False.

  3. Use the getter and setter functions as frb suggests. These are also only present because they are part of the C API, but some people prefer them over the props attribute. Also, there is no guarantee that any particular property will have getter and setter functions! Usually in a well-designed C API they will be there, but it is not required.

For style properties, I believe the only option is #1. For "attributes", well, these are simply Python attributes. To access the allocation attribute, use btn1.allocation.



回答2:

In PyGTK, GtkWidget is the base class that all other widget classes (including ones you might make yourself) inherit from.

As far as setting properties goes, you probably noticed you can't set them directly:

btn1.label = "StackOverflow"

In PyGTK, you need to prefix the names of the properties with set_, like this:

btn1.set_label("StackOverflow")

If there's a - in the property name, like with use-underline, turn them into underscores, like set_use_underline. I'd like to say that I don't think this use of getters and setters is very pythonic.

Here's a full working program, taken from the ZetCode tutorial and modified.

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()


回答3:

You can change a Widget property by using the Gtk.Widget.set_property(property, value) method. property should be a string.



回答4:

To get all the widget there widget.pros list:

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

Output:

<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'>