Working with wxSize and wxPoint

The list of wx.Frame constructor arguments (table 2.1) also referenced the classes wx.Size and wx.Point. These two classes will be used frequently in your wxPython programming. Because of that, some Python-specific shortcuts have been added to the classes.

The wx.Size and wx.Point classes are quite similar both in their wxWidgets method list and in how they are represented in wxPython. The wx.Point class, surprisingly enough, represents a point or position. The constructor takes two arguments for the x and y values of the point. The values both default to zero if not set:

To set both dimensions in one line, use the function Set(x,y). To retrieve them both at once, use Get(), which returns the two values as a Python tuple. Unlike most wxWidgets classes, wx.Point has functionality defined to retrieve the x and y values as simple Python properties such that the x and y values are accessible like ordinary Python attributes:

In addition, wx.Point instances can be transparently added, subtracted, and compared just like other Python objects. For example:

x = wx.Point(2, 3) y = wx.Point(5, 7) z = x + y bigger = x > y

In a wx.Point instance, the attributes are expected to be integers. If you need floating-point coordinates, you can use the class wx.RealPoint, which works much the same way as wx.Point.

The wx.Size class is almost identical to wx.Point, except that the instance variables are named width and height instead of x and y. Otherwise, it has the same attribute and operator features defined.

When a wx.Point or wx.Size instance is needed anywhere in your wxPython program—for example, in the constructor for another object—you do not need to create the instance explicitly. Instead, you can pass a Python tuple to the constructor, and wxPython will implicitly create the wx.Point or wx.Size instance:

frame = wx.Frame(None, -1, pos=(10, 10), size=(100, 100))

This works not just in constructors for other objects, but also anyplace where a wx.Point or wx.Size is expected by a wrapped C + + method or function call. You can even write something like this, if you so desire:

frame.SetPosition((2, 3))

Was this article helpful?

0 0

Post a comment