Creating New Widgets

  1. Customizing Existing Widgets
  2. Deriving from widgets.Base
  3. Deriving from Window

Customizing Existing Widgets

Since all the widgets in PyUI are written entirely in python it is extremely easy to customize their behavior and their appearance. To modify the way widgets draw themselves, implement the draw() method and use the drawing methods provided by the Theme. Widget drawing is done through the Theme so that a common look is preserved for all widgets. If the functionality provided by the them-drawing interface isn't flexible enough for your widget, then you can use the drawing primative methods of the renderer. Be aware though, that your new widget will not be themed correctly when you use the core drawing methods.

The generic PyUI Theme drawing methods are:

    def drawText(self, text, position, color)
    def draw3DRect(self, rect, color, reverse)
    def shadeBorder(self, rect, d, reverse)
    def drawOutlineRect(self, rect, color, thick=1)
    def drawImage(self, rect, image)
    def drawRect(self, rect, color)
    def drawGradient(self, rect, vertical, c1, c2)
    def drawBack1(self, rect)
    def drawBack2(self, rect)
    def drawBack3(self, rect)

There are also widget specific drawing methods that are probably less useful for customizing widgets:

    def drawButton(self, rect, title, hasFocus, status, enabled)
    def drawImageButton(self, rect, filename, title, hasFocus, status)
    def drawLabel(self, rect, title, color)
    def drawEdit(self, rect, text, hasFocus, caretPos, selectPos)
    def drawFrame(self, rect, title)
    def drawScrollButtonUp(self, rect)
    def drawScrollButtonDown(self, rect)
    def drawScrollBar(self, rect)
    def drawTabItem(self, rect, title, active)
    def drawTabHeader(self, rect)
    def drawMenuBar(self, rect)
    def drawMenuBarItem(self, rect, title, selected)
    def drawMenu(self, rect)
    def drawMenuItem(self, rect, title, selected, icon = None)
    def drawListBoxItem(self, rect, title, selected, color)
    def drawSplitter(self, rect)
    def drawToolTip(self, text, rect)

When customizing a widget is common to create a new python class that is derived from one of the existing PyUI widget classes. When the draw() method is passed a reference to the renderer. Use this renderer to call drawing methods for new widgets. Below is an example of creating a new EditBox widget that displays an image in its background:

import pyui
class ImageEdit(pyui.widgets.Edit):
    def __init__(self, text, image):
        pyui.widgets.Edit.__init__(self, text, 30,None)
        self.image = image
    def draw(self, renderer):
        renderer.drawImage(self.windowRect, self.image)
newFrame = pyui.widgets.Frame(10, 10, 250, 200, "Show a list")
edit1 = pyui.widgets.Edit("this is text",20, None)
edit2 = ImageEdit("text over an image", "back.bmp")



(C) Sean Riley 2002