Simple window manager for PyS60
by Marcelo Barros
Some days ago I was asked about a PyS60 application that should support several bodies but without using tabs. The idea was to create a dynamic menu with an option called “Switch to” from which the user could choose the desired body.
After some frustrated experiences I wrote the following code for this special “Window Manager”. It is very compact and Pythonic (at least in my opinion) and I decided to share it with our readers.
""" Simple window manager for PyS60 Marcelo Barros de Almeida <marcelobarrosalmeida@gmail.com> License: GPL3 """ from appuifw import * import e32 import copy class Window(object): __lock = e32.Ao_lock() __windows = [] def __init__(self,**kargs): self.__dict__.update(kargs) Window.__windows.append(self) def run(): Window.__windows[0].switch() Window.__lock.wait() run = staticmethod(run) def switch(self): op = [] for s in Window.__windows: if s != self: op.append((s.title,s.switch)) cm = [(u"Switch to",tuple(op))] cm.append((u"Exit",Window.exit)) app.menu = self.menu + cm app.body = self.body app.title = self.title def exit(): Window.__lock.signal() exit = staticmethod(exit)
The core of this window manager is the function switch, responsible to recreate the menu and to list all other bodies (except the current one). Using a static list of bodies and a test based on self, it is simple to do this task. Exit option is added at the end.
Each body is constructed just calling Window with, at least, body, title and menu parameters. It was used a nice trick based on dictionary updating (self.__dict__.update(kargs)) in order to add all parameters to the current object, avoiding additional tests or definitions. Of course you need to be careful when creating new bodies. Moreover, run and exit are static methods since they can be called from any place of your code.
Usage and screenshots:
# Example a = Window(title=u"Text editor", body=Text(u"Type here"), menu=[(u"Text 1",lambda:None), (u"Text 2",lambda:None), (u"Text 3",lambda:None)]) b = Window(title=u"Single list", body=Listbox([u"item"]), menu=[(u"Slist 1",lambda:None), (u"Slist 2",lambda:None), (u"Slist 3",lambda:None)]) c = Window(title=u"Dual list", body=Listbox([(u"item",u"item")]), menu=[(u"Dlist 1",lambda:None), (u"Dlist 2",lambda:None), (u"Dlist 3",lambda:None)]) Window.run()
Related posts:
- A simple Progressbar implementation in Python (Part II) Following the last post, here's the other implementation of ProgressBar,...
- PyS60 UI tutorials, part 1: Text and Canvas I've recently written some articles about UI controls on the...
- A simple Progressbar implementation in Python A quick post just to share a small class to...
Related posts brought to you by Yet Another Related Posts Plugin.