Editing GtkWidget attributes/properties
Asked Answered
I

4

3

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

Impart answered 2/3, 2013 at 22:44 Comment(0)
C
4

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.

Colicroot answered 3/3, 2013 at 9:50 Comment(0)
S
2

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'>
Sender answered 22/10, 2013 at 22:10 Comment(0)
P
1

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()
Padraig answered 2/3, 2013 at 23:11 Comment(1)
Yes, I know this, but take for example pygobject's Gtk.RadioToolButton, which doesn't have a seperate join_group() method, except a method that creates a new RadioToolButton and adding it to a group. If I want to modify an existing RadioToolButtons group, I will need to modify a property (which I've just found out how to do ;) (see my answer)).Impart
I
1

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

Impart answered 2/3, 2013 at 23:34 Comment(1)
it is actually a GObject method, which will work on objects that are not widgets as gtk.TextTag for example.Hopson

© 2022 - 2024 — McMap. All rights reserved.