PyQt by example (Session 2)
I am finally publishing my LatinoWare 2008 tutorial, in revised and expanded form. It will probably be a 10-part series, and here is session 2.
See also session 1.
I am finally publishing my LatinoWare 2008 tutorial, in revised and expanded form. It will probably be a 10-part series, and here is session 2.
See also session 1.
![]() |
Review:Yes, I really liked it. I am a bit exhausted though, having read all 6 uplift books in a couple of months. |
The uRSSus doc is slowly growing, so I hooked assistant to the UI. Not difficult, but I have not seen it elsewhere.
Here's how:
Assume the "Handbook action" is called action_Handbook. Set window.assistant to None in __init__.
def on_action_Handbook_triggered(self, i=None):
if i==None: return
if not self.assistant or \
not self.assistant.poll()==None:
helpcoll=os.path.join(os.path.abspath(os.path.dirname(__file__)),\
'help',\
'out',\
'collection.qhc')
cmd="assistant -enableRemoteControl -collectionFile %s"%helpcoll
self.assistant=subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE)
self.assistant.stdin.write("SetSource qthelp://urssus/doc/handbook.html\n")
And that's it. Now I ned to figure out context help.
Why? It's basically undocumented, it looks ugly and it doesn't support template inheritance, which in this case is very useful, because I am actually doing two very similar planets: 1 2.
So, since I saw a plugin to use Vellum templates, I thought, why not use my favourite templating library (Mako) instead?
It turns out to be very easy to do!
Just put this in your plugins folder and start using Mako templates (yes, the planet's config and stuff is in github. Why not?).
# -*- coding: utf-8 -*-
import rawdoglib.plugins
from mako.template import Template
from mako.lookup import TemplateLookup
from mako import exceptions
class MakoPlugin:
def __init__(self):
self.lookup = TemplateLookup(directories=['.'],\
output_encoding='utf-8',\
encoding_errors='replace')
def fill_template(self,template, bits, result):
self.lookup.put_string('actual',template)
t=self.lookup.get_template('actual')
try:
r=t.render_unicode(**bits)
except:
r=exceptions.html_error_template().render()
result.value=r
return False
p = MakoPlugin()
rawdoglib.plugins.attach_hook("fill_template",\
p.fill_template)
Yup, 20 lines of code!