The Tkinter 3000 RightArrow Widget
May 9, 2001 | Fredrik Lundh
This widget is an enhanced version of the SimpleRightArrow demo widget. This version provides three custom options, and it also precalculates graphics resources and coordinates.
from WCK import Widget, FOREGROUND class RightArrow(Widget): # widget implementation ui_option_foreground = FOREGROUND ui_option_width = 200 ui_option_height = 200 def ui_handle_config(self): # precalculate brush resource self.brush = self.ui_brush(self.ui_option_foreground) return int(self.ui_option_width), int(self.ui_option_height) def ui_handle_resize(self, width, height): # precalculate polygon self.arrow = (0, 0, width, height/2, 0, height) def ui_handle_repair(self, draw, x0, y0, x1, y1): draw.polygon(self.arrow, self.brush)
The ui_option_foreground class attribute provides two things: it tells the WCK that this widget supports an option called foreground, and it also provides a default value for that option (in this case, the default value is taken from the WCK.FOREGROUND variable, which contains a suitable default for the current platform). The ui_option_width and ui_option_height does the same for the width and height options.
The ui_handle_config() method is called when the widget is created, and whenever any of the options are changed. It creates a new brush based on the foreground option, and returns the requested width and height, in pixels. Note that users may use strings also for the size options, so this method uses int() to be on the safe side.
The ui_handle_resize() method is called when the widget is created, and whenever it’s resized (either by a geometry manager, or if ui_handle_config requests a new size). It is used to calculate the arrow outline.
The ui_handle_repair() method, finally, is called when the widget needs to be updated. It draws a polygon using the brush and coordinates set by the other two methods.