In most pygtk widget pages, they contain sections called 'Attributes', 'Properties', and 'Style Properties'. How can I change these properties and attributes?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can change a Widget property by using the
Gtk.Widget.set_property(property, value)
method.property
should be a string.To get all the widget there widget.pros list:
Output:
There are three ways to change properties:
As in zheoffec's answer, use the
set_property()
function (orset_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.Use the
props
attribute. Any property that you find in the documentation can be accessed through this attribute. For example,btn1.props.label = 'StackOverflow'
andbtn1.props.use_underline = False
.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, usebtn1.allocation
.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:
In PyGTK, you need to prefix the names of the properties with
set_
, like this:If there's a
-
in the property name, like withuse-underline
, turn them into underscores, likeset_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.