Close event on xbmcgui.WindowXML
#1
Hi,

how can I detect a window close event in Python?

I want to stop the timer when I go to another window (not in the onClick or Timeout).

This is my code. I have a progress bar, a timer lib and a button.

Code:
class ScreenTest(xbmcgui.WindowXML):
    control_id_button_action = 3000
    control_id_progress_timeout = 3010

    def __init__(self, *args, **kwargs):
        xbmcgui.WindowXML.__init__(self, *args, **kwargs)

    def onInit(self):
        self.button_action = self.getControl(self.control_id_button_action)
        self.progress_timeout = self.getControl(self.control_id_progress_timeout)

        rt = RepeatedTimer(0.5, self.incrementData, [self, "hola"])
        rt.start()

  def incrementData(self, name):
        percent = self.progress_timeout.getPercent()
        if(percent == 100):
            self.close()
        else:
            self.progress_timeout.setPercent(percent+1)

   def onClick(self, controlId):
        if controlId == self.control_id_button_action:
            self.doAction()

   def doAction(self):
            import resources.lib.SecondScreen as Screen
            ui = Screen.SecondScreen ('SecondScreen.xml', addon.get_path(), 'default')
            ui.doModal()
            del ui

Thanks in advance
Reply
#2
Hi,

here is my own solution. As I can see there is no onDeInit or onClose event in the window object so I use a work around solution.

First of all I use a Timer class I found.

Code:
from threading import Timer

'''
from time import sleep

def hello(name):
    print "Hello %s!" % name

print "starting..."
rt = RepeatedTimer(1, hello, "World") # it auto-starts, no need of rt.start()
try:
    sleep(5) # your long-running job goes here...
finally:
    rt.stop() # better in a try/finally block to make sure the program ends!

'''
class RepeatedTimer(object):
    def __init__(self, interval, function, *args, **kwargs):
        self._timer     = None
        self.interval   = interval
        self.function   = function
        self.args       = args
        self.kwargs     = kwargs
        self.is_running = False
        self.start()

    def _run(self):
        self.is_running = False
        self.start()
        self.function(*self.args, **self.kwargs)

    def start(self):
        if not self.is_running:
            self._timer = Timer(self.interval, self._run)
            self._timer.start()
            self.is_running = True

    def stop(self):
        self._timer.cancel()
        self.is_running = False

Them, onInit event I create the timer and start it.

Code:
self.rt = RepeatedTimer(1, self.incrementData, [self, "hola"])
self.rt.start()

and the progress function is this one.

Code:
def incrementData(self, name):
        percent = self.progress_timeout.getPercent()
        if(percent == 100):
            self.rt.stop()
            self.close()
        else:
            self.progress_timeout.setPercent(percent+1)

Them when I press a Button or we arrive to the 100% of the progress bar, I stop the time and close the window.
Code:
self.rt.stop()
            self.close()

I think an event in the window like onSuspend,when we got to another window or onDestroy when we close the window can be very useful for this situations.

If anyone has a better solution please, say me.

Thanks
Reply

Logout Mark Read Team Forum Stats Members Help
Close event on xbmcgui.WindowXML0