2014-12-11, 18:48
Currently I'm researching a possibility to update a xbmcgui Control from a background thread to display some live data in an addon window. So I've modified my second PyXBMCt example in the following way:
Everything works as expected: the addon window pops-up and the label shows a live count from 0 to 10. But I wonder if this approach is thread-safe and does not pose any hidden dangers.
PHP Code:
import time
import threading
# Import PyXBMCt module.
import pyxbmct.addonwindow as pyxbmct
class MyWindow(pyxbmct.AddonDialogWindow):
def __init__(self, title=''):
# You need to call base class' constructor.
super(MyWindow, self).__init__(title)
# Set the window width, height and the grid resolution: 2 rows, 3 columns.
self.setGeometry(350, 150, 2, 3)
# Create a text label.
self.label = pyxbmct.Label('', alignment=pyxbmct.ALIGN_CENTER)
# Place the label on the window grid.
self.placeControl(self.label, 0, 0, columnspan=3)
# Create a button.
button = pyxbmct.Button('Close')
# Place the button on the window grid.
self.placeControl(button, 1, 1)
# Set initial focus on the button.
self.setFocus(button)
# Connect the button to a function.
self.connect(button, self.close)
# Connect a key action to a function.
self.connect(pyxbmct.ACTION_NAV_BACK, self.close)
update_thread = threading.Thread(target=self.update_label)
update_thread.daemon = True
update_thread.start()
def update_label(self):
for i in range(11):
self.label.setLabel('Count: {0}'.format(i))
time.sleep(1.0)
# Create a window instance.
window = MyWindow('Hello, World!')
# Show the created window.
window.doModal()
# Delete the window instance when it is no longer used.
del window
Everything works as expected: the addon window pops-up and the label shows a live count from 0 to 10. But I wonder if this approach is thread-safe and does not pose any hidden dangers.