doModal() window can't self.close()
#1
Hi. I try to close window and script and I can't. Simple script:

Code:
ACTION_PREVIOUS_MENU = 10
ACTION_SELECT_ITEM = 7

class MyClass(xbmcgui.Window):
  def __init__(self):
    self.strActionInfo = xbmcgui.ControlLabel(100, 120, 200, 200, '', 'font13', '0xFFFF00FF')
    self.addControl(self.strActionInfo)
    self.strActionInfo.setLabel('Push BACK to quit - A to reset text')
    self.close() # THIS LINE DON'T WORK
    
  def onAction(self, action):
    if action == ACTION_PREVIOUS_MENU:
      self.close()

mydisplay = MyClass()
mydisplay .doModal()
del mydisplay
Reply
#2
__init__(self) constructor is executed on class instantiation, but only after doModal() a Window instance is actually displayed and goes into an event loop, when it can be closed with close(). Your first self.close() call is definitely out of place.
To display a Window instance without entering an event loop you can call show(), but it will last only as long as your script runs, i.e. with show() you need either to call doModal eventually to go into an event loop, or organize your own event loop in your script.
Reply
#3
Now it is much clearer to me. But I have another problem. I run the script using the keyboard shortcut by calling runScript(ID). When the window created by doModal () I again press the shortcut, then appears the same window not closing previous. Is there a way to prevent this? Is it possible to give a unique id for the window and then at the beginning of the script to check if there exist? Or is there some other way?
Reply
#4
AFAIK keyboard actions, even intercepted by a script, are still propagated to the core XBMC event loop. An ugly way that I can think of very quickly is to create an empty temporary file on script start and delete it on script stop, so that the next script instance could check this file with os.path.exists() and close immediately, if True.

BTW, If you want to create a pure Python GUI for your addon (without an XML skin), I'd recommend you to try my PyXBMCt framework: http://forum.xbmc.org/showthread.php?tid=174859
Reply
#5
There is one problem. When the script creates a temporary file and XBMC will close without completing the script. The temporary file is not deleted, and the script will not run it anymore.

Thanks for the suggestion but for now I am learning python and write a simple script with one small window.
Reply
#6
Maybe something like this -

Code:
class MyClass(xbmcgui.Window):
  def __init__(self):
    self.strActionInfo = xbmcgui.ControlLabel(100, 120, 200, 200, '', 'font13', '0xFFFF00FF')
    self.addControl(self.strActionInfo)
    self.strActionInfo.setLabel('Push BACK to quit - A to reset text')
    
  def onInit(self):
    self.window = xbmcgui.Window(xbmcgui.getCurrentWindowId())
    self.window.setProperty('MyAddonIsRunning', 'true')
    
  def onAction(self, action):
    if action == ACTION_PREVIOUS_MENU:
      self.close()

window = xbmcgui.Window(xbmcgui.getCurrentWindowId())
  if window.getProperty('MyAddonIsRunning') != 'true':
    mydisplay = MyClass()
    mydisplay .doModal()
    del mydisplay
Reply
#7
Thanks a lot. onInit() not work for me. I did it this way:

Code:
class MyClass(xbmcgui.Window):
  def __init__(self):
    self.window = xbmcgui.Window(xbmcgui.getCurrentWindowId())
    self.window.setProperty('MyAddonIsRunning', 'true')
    
    self.strActionInfo = xbmcgui.ControlLabel(100, 120, 200, 200, '', 'font13', '0xFFFF00FF')
    self.addControl(self.strActionInfo)
    self.strActionInfo.setLabel('Push BACK to quit - A to reset text')
      
  def onAction(self, action):
    if action == ACTION_PREVIOUS_MENU:
      self.close()

window = xbmcgui.Window(xbmcgui.getCurrentWindowId())
if window.getProperty('MyAddonIsRunning') != 'true':
    mydisplay = MyClass()
    mydisplay .doModal()
    del mydisplay
    window.setProperty('MyAddonIsRunning', 'false')
Reply

Logout Mark Read Team Forum Stats Members Help
doModal() window can't self.close()0