14th Feb 2008

VensterCE: Hello World, again

In this tutorial, we are actually going to make a window, although we will still just be saying “Hello, World”

The Code

from venster.ce import *

class MyWindow(CeMainWindow):
    _window_title = u"Hello World, again"

    @msg_handler(WM_CREATE)
    def OnCreate(self,event):
        self.sizer = BoxSizer(VERTICAL)
        text = StaticCenter(u"Hello World", parent=self)
        self.sizer.append(text)
        CeMainWindow.OnCreate(self,event)

def main():
    mainForm = MyWindow()
    mainForm.ShowWindow()
    application = Application()
    appliation.Run()

if __name__ == "__main__": main()

Step by Step

class MyWindow(CeMainWindow):
    _window_title = u"Hello World, again"

This is the class definition of the main window. It inherits from CeMainWindow, which was imported from venster.ce at the top. “_window_title” is a class variable, and representing the title of your window. Once again, be sure to use unicode (prepend the string literal with a “u” or convert with the unicode(str) function), or it will come out looking like garbage.

    @msg_handler(WM_CREATE)
    def OnCreate(self,event):
        self.sizer = BoxSizer(VERTICAL)
        text = StaticCenter(u"Hello World", parent=self)
        self.sizer.append(text)
        CeMainWindow.OnCreate(self,event)

the @msg_handler(WM_CREATE) is a decorator function, setting this function up as the message handler for the WM_CREATE function; the effect being that this function is called as soon as the window is created.
self.sizer (the name is important) is our main widget sizer — all widgets we create should be appended to it, or one of its children.
There are 4 classes of static text: Static, StaticLeft, StaticCenter, and StaticRight. They all take the same arguments.
Finally we call the parent class’ OnCreate to finish setting up the window.

def main():
    mainForm = MyWindow()
    mainForm.ShowWindow()
    application = Application()
    appliation.Run()

if __name__ == "__main__": main()

These lines of code will probably be at the end of all your programs; they “create” the window and start th eapplication running.

Leave a Reply