Log in

View Full Version : Gloabl varibales and Onload event


mridang
July 24th, 2009, 05:52 AM
Hi Guys,

I'm writing a Boxee application. It uses a lot of embedded python code. This Python code makes a lot of calls to another python class that i've written. I would like to create an instance of a class and have it available as a global object. This way i can reference the same object from code in different onlick event locations scattered across the XML file.

1. Where do i create an onload even for my application?
2. How do i create Python global variables in my XML file. Where would this code have to be written.

I hope that some guru on this forum would know. :)

Thanks for any help guys.

hideki23
July 24th, 2009, 08:14 PM
i had problems using the onload event. it wouldn't execute my python script, so as a workaround i added a little splashscreen with a button and an onclick event. ^^

for the global variable you can use localconfig. it might be ugly but works just fine. although i don't know how to delete the stored values if the app is closed.

storing a value:
mc.GetApp().GetLocalConfig().SetValue("pagecounter", "1")

reading a value:
mc.GetApp().GetLocalConfig().GetValue("pagecounter")

these are string values, if you need some other datatypes in your python script just store them as strings and convert them when you access them. ^^

DPK
July 25th, 2009, 01:03 PM
Where are you trying to do the onload? It should be near the top of your applications xml file like this:

<?xml version="1.0"?>
<window id="14000">
<defaultcontrol always="true">1200</defaultcontrol>
<allowoverlay>no</allowoverlay>
<onload lang="python"><![CDATA[
from resources.lib import myapp
myapp.getMain()
]]></onload>

I've never liked mixing my python and the xml in the same place so try to keep it at a minimum and just call functions w/ onload, onclick, etc. You could however write pretty much your entire app w/ pythin in the xml files.

Check your boxee.log for errors and remember that the python code needs to be aligned all the way to the left in order for it to function correctly.