The Widget Construction Kit (WCK) is an extension API that allows you to implement all sorts of custom widgets, in pure Python.
For more information on the Widget Construction Kit, see the WCK page over at effbot.org.
Here's a very simple example. This widget displays the text "hello, world" in the upper left corner:
class HelloWorld(Widget):
def ui_handle_repair(self, draw, x0, y0, x1, y1):
font = self.ui_font("black", "times")
draw.text((0, 0), "hello, world", font)
widget = HelloWorld(root)
widget.pack()
You can override the following Widget hook methods:
Standard controller for the ButtonMixin class and other button-like widgets.
For more information about this class, see The ButtonController Class.
Button mixin.
For more information about this class, see The ButtonMixin Class.
Standard button widget base class.
For more information about this class, see The ButtonWidget Class.
Controller base class.
For more information about this class, see The Controller Class.
Abstract 2D drawing interface.
For more information about this class, see The DrawInterface Class.
Standard controller for the EventMixin class.
For more information about this class, see The EventController Class.
Event mixin.
For more information about this class, see The EventMixin Class.
Creates a controller for the given widget.
Standard observable mixin.
For more information about this class, see The Observable Class.
Scroll mixin.
For more information about this class, see The ScrollMixin Class.
Simple scrolled event-handling widget base class.
For more information about this class, see The SimpleWidget Class.
Experimental style object.
For more information about this class, see The Style Class.
Text mixin.
For more information about this class, see The TextMixin Class.
The WCK widget base class.
For more information about this class, see The Widget Class.
Standard controller for the ButtonMixin class and other button-like widgets.
Button mixin. This mixin implements basic button widget behaviour (arm/click).
Called when the user presses the button, either by clicking the mouse button over the button, or by pressing the space bar.
The default implementation calls the object given by the command option, if callable.
Called when the mouse button is pressed with the mouse placed over the button.
Called when the mouse button is released after the mouse has been moved out of the button.
Called when the mouse pointer is moved over the button.
Called when the mouse pointer is moved out from the button.
Standard button widget base class. This widget inherits from ButtonMixin, and adds a relief option, which is changed to "sunken" whenever the button is armed.
Controller base class. This class provides a standard implementation of the controller class. To implement a custom controller, create a subclass to this class and implement the create method.
Creates a controller. This method must be overridden by the implementation class.
Binding tag for this controller.
(Internal) Calculate a unique binding tag for this controller instance.
Abstract 2D drawing interface.
All WCK implementations provide a standard 2D drawing interface. By sticking to this interface, your widgets will work on all platforms supported by the WCK.
Note that different implementations use difference classes, and it isn't always a class either. Make sure your widgets only relies on the interface provided by the drawing object, not the exact implementation.
(Experimental, not supported in WCK 1.0) Draws a circle.
Extracts a subrectangle from an image object or pixmap.
Example:
draw.paste(image.crop(source), dest)
Draws an ellipse.
Draws a line.
Examples:
# a cross pen = widget.ui_pen(red) draw.line((x0, y0, x1, y1), pen) draw.line((x0, y1, x1, y0), pen) # a character draw.line((0, 0, 20, 100, 30, 50, 40, 100, 60, 0), pen)
Draws an image or pixmap object.
Draws a polygon.
If a brush is given, it is used to fill the polygon. If a pen is given, it is used to draw an outline around the polygon. Either one (or both) can be left out.
Draws a rectangle.
If a brush is given, it is used to fill the rectangle. If a pen is given, it is used to draw an outline around the rectangle. Either one (or both) can be left out.
Sets the drawing transform. Note that the transform is reset before each call to ui_handle_repair.
Example:
draw.settransform((dx, dy))
Draws text.
Draws a text string at the given position, using the given font.
Example:
font = widget.ui_font(black, times) draw.text((100, 100), "hello, world", font)
Determines the size of a text string.
Standard controller for the EventMixin class.
Event mixin. This mixin simplifies user event handling, by mapping mouse and keyboard events to DOM/DHTML-style method calls.
Note that this mixin overrides the ui_controller attribute.
Called for mouse clicks.
Called for keyboard events.
Called for mouse button press events.
Called for mouse motion events.
Called for window leave events.
Called for window enter events.
Called for mouse button release events.
Standard observable mixin. This mixin class can be used to make container objects (including widget models) "observable".
Add an observer object to the container. The observer should be a callable object which takes two arguments (the event code and associated data).
Send the event and associated data to all observers. If an exception occurs in an observer, notification is aborted and the exception is propagated back to the caller.
Remove the given observer object from the container. The observer must exist.
Scroll mixin. This mixin implements vertical and horizontal scrolling.
Update scrollbar.
Get total/left/right indexes.
Set left margin to left + units + pages.
Get total/top/bottom indexes.
Set top margin to left + units + pages.
Change the horizonal view.
Change the vertical view.
Simple scrolled event-handling widget base class. This class inherits from ScrollMixin and EventMixin.
Experimental style object.
Draws the widget background.
Draws the widget border.
Gets the margin width.
Text mixin. This mixin class maps foreground and font options to a font resource attribute, and handles width and height in character units.
To define what should be drawn, override the ui_text_get method.
Default config method. This implementation sets up the font attribute and the widget size, based on the mixin options. If you override this method, you must remember call the mixin version.
Default repair method. This implementation calls the ui_text_draw method.
Draws centered text. The default implementation draws centered text, and uses the ui_text_get method to determine what to draw.
Get text to draw. This method is only called if ui_text_draw is called without a text argument.
The WCK widget base class. This class implements a generic WCK widget, which should be subclassed to provide drawing and event handling behaviour.
Creates a widget instance.
This method simply calls the ui_init method. If you need to initalize instance variables, you can override this method. You must remember to call ui_init when done (or if you prefer, call Widget.__init__).
Sets the value of a single configuration option.
Gets the value of a widget configuration option.
Configures a widget instance.
Destroys a widget instance.
Gets a list of all available options.
(Experimental) Attach a geometry manager to this widget.
Creates a brush object with the given characteristics. The brush can only be used in this widget.
(Class attribute) Standard event controller for this widget.
Reports widget damage. This will force the widget to redraw all or parts of it's screen estate.
(Class attribute) Control double buffering for this widget. If set to a true value, the ui_handle_repair method will be set up to draw in an off-screen buffer, which is then copied to the screen in one step.
(Instance attribute) (Experimental) Drawing context for this widget.
Creates a font object with the given characteristics, for use in this widget.
A font specifier. This should be a string with the following syntax: "{family} size style...".
If the family name doesn't contain whitespace, you can leave out the braces. If omitted, the family name defaults to courier.
The size is given in points (defined as 1/72 inch). If omitted, it defaults to 12 points. Note that the toolkit takes the logical screen size into account when calculating the actual font size. On low resolution screens, this means that a 12-point font is usually larger than 12/72 inches.
The style attributes can be any combination of normal, bold, roman (upright), italic, underline, and overstrike. If omitted, it defaults to the default setting for that family; usually normal roman.
For Tkinter compatibility, you can also give the font as a tuple: ("family", size, style...). Note that there should be no braces around the family name. You can also leave out the size and/or the style arguments. The defaults are the same as for the string syntax.
(Hook) Called by the framework to clear a portion of this widget. The default implementations fills the background with the current background style. If you're drawing the background in the repair method, you should override this method with an empty implementation.
(Hook) Called by the framework when this widget has been reconfigured. This method should check configuration options (ui_option attributes), and update widget attributes as necessary.
(Hook) Called by the framework when some part of this widget has been damaged, and will have to be redrawn. This method will always be called at least once before each call to ui_handle_repair.
(Hook) Called by the framework when this widget is about to be destroyed.
(Hook) Called by the framework when this widget has received or is about to loose focus.
(Hook) Called by the framework when this widget should be redrawn. This call will always be preceeded by one or more calls to ui_handle_damage, and a single call to ui_handle_clear.
(Hook) Called by the framework when this widget has been resized, either by a geometry manager, or by the user.
(Hook) Called by the framework when this selection status has changed. In the current implementation, this handler is never called.
Creates an image object with the given characteristics. The image (or a cropped subregion of it) can be pasted onto a a window or a pixmap.
Note that image descriptors are not cached.
Also note that in the current version, this method returns a pixmap. This may change in future versions.
Initializes a widget instance.
(Class attribute) Background color.
(Class attribute) Border width. If not zero, the border is decorated according to the relief setting.
(Class attribute) Cursor to use when the mouse is moved over this widget. If empty, the default cursor is used.
(Class attribute) Focus region width.
(Class attribute) Border relief.
Converts a coordinate list to a more efficient representation. This method can be used to "compile" coordinate lists.
Creates a pen object with the given characteristics. The pen can only be used in this widget.
Creates an pixmap object with the given characteristics.
Clears the resource cache. Clears the cache for this widget (and any other widgets that may share the same cache). Resources that are cached as instance attributes are not affected.
If this method is called from the constructor, before the ui_init is called, caching is disabled for this widget. Widgets that use large numbers of resources (e.g. font and colour browsers) should disable the cache.
Gets the current size of this widget.