[PyKDE] pykde newbie--several questions
Paul Giannaros
ceruleanblaze at gmail.com
Thu Jan 4 22:29:52 GMT 2007
On Thursday 04 January 2007 22:20, Kenneth McDonald wrote:
> Paul,
>
> Thanks for making all of this clear! I'd definitely appreciate some
> example event code (or for that
> matter, code that uses the DOM to implement a small application), since
> my goal is to use the
> DOM as a UI.
>
> I get the feeling that KDE actually uses is DOM renderer for a lot of
> its own applications. Would
> that be correct?
>
No, probably not. It is used in small places (KMail uses it to show HTML
e-mail, Amarok uses it when it shows wikipedia info, and Kopete uses it for
chat windows), but most applications use the regular PyKDE stuff to create
their own GUI. It's faster, more robust, and gives you a more desktopy feel
(standard font sizes / colours / background colours are used, etc). With that
said, you can create your own GUI with HTML if you'd like, there's nothing
stopping you.
To listen for an event, first get a reference to the object you want to listen
on. For example, create a page that contains an anchor tag and an ID (i.e <a
id="link">Blah blah</a>), then load that page in your KHTMLPart and call
`myLink = khtmlpart.htmlDocument().getElementById(DOMString("link"))` to get
the reference. You need to create a khtml.DOM.EventListener subclass and
reimplement the handleEvent method, for example with something like:
class Listener(khtml.DOM.EventListener):
def handleEvent(self, event):
print "Click!"
Finally, hook up the event listener with your link so it's called onclick:
myLink.addEventListener("click", Listener(), False)
There may be the odd typo, but that gives you the idea.
>
> Thanks,
> Ken
>
> Paul Giannaros wrote:
> > On Thursday 04 January 2007 21:32, Kenneth McDonald wrote:
> >>>> 2) It appears that pykde can access the DOM inside an HTML window in a
> >>>> great deal of detail. Is it possible to write pykde scripts that
> >>>> create an HTML window, receive events from the window, and then update
> >>>> the DOM of the window 'live' so that the user sees their action
> >>>> reflected in the contents of the window? To put it more simply, is it
> >>>> possible to script applications that simply use the HTML DOM as the
> >>>> user interface?
> >>>
> >>> I don't think KHTML exposes access to the DOM if thats what you mean.
> >>
> >> This PyKDE doc page in particular:
> >>
> >>
> >> http://www.riverbankcomputing.com/Docs/PyKDE3/classref/khtml/DOM.Node.ht
> >>ml
> >>
> >> made me wonder if direct DOM access was possible. At the moment I don't
> >> have Kubuntu set up, so can't experiment.
> >
> > Yes, it is. I do it myself fairly often. From your KHTMLPart instance you
> > can access the htmlDocument() attribute and from there walk the DOM, use
> > methods like getElementById/getElementsByTagName, etc. The main thing to
> > remember is that when those functions ask for string input they require
> > DOMStrings
> >
> > instead of regular strings. Quick DOMString examples:
> >>>> import khtml
> >>>> domString = khtml.DOM.DOMString("foo bar")
> >>>> qString = domString.string() # the string() attribute of a
> >>>> DOMString
> >
> > returns a QString
> >
> >>>> pythonString = str(qString)
> >
> > As long as you follow the documentation and read the class information
> > you should be okay.
> > You can also set up event handling (i.e catch an onclick event on a
> > button or link and then run some Python code), though that's a little
> > more involved. I can give you an example if you'd like.
> >
> >> Thanks,
> >> Ken
> >>
> >> _______________________________________________
> >> PyKDE mailing list PyKDE at mats.imk.fraunhofer.de
> >> http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
> >
> > _______________________________________________
> > PyKDE mailing list PyKDE at mats.imk.fraunhofer.de
> > http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
More information about the PyQt
mailing list