GUI tutorial: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
(Created page with "{{mininav|Development|Add-on development|Python development|Python examples}} <br /> =Introduction= This How To will explain several ways of showing a "Hello...")
 
m (Bot: Automated text replacement (- XBMC + Kodi ); cosmetic changes)
Line 2: Line 2:
<br />
<br />


=Introduction=
= Introduction =


This How To will explain several ways of showing a "Hello World" popup on screen
This How To will explain several ways of showing a "Hello World" popup on screen


=Examples=
= Examples =


==Using xbmc modules==
== Using xbmc modules ==


Simple example using xbmc python modules showing a notification dialog that will show for 5 seconds.
Simple example using xbmc python modules showing a notification dialog that will show for 5 seconds.


===using build-in functions===
=== using build-in functions ===
More info on Built-In functions can be found here: [[List_of_built-in_functions]]
More info on Built-In functions can be found here: [[List of built-in functions]]
We will two examples. One with just a simple notification dialog and the other with the same dialog but with using variables.
We will two examples. One with just a simple notification dialog and the other with the same dialog but with using variables.
It all depends on how extended you scripts will be. Since we require all used strings to be localized (translatable) we recommend the second option.
It all depends on how extended you scripts will be. Since we require all used strings to be localized (translatable) we recommend the second option.
Line 28: Line 28:
import xbmcaddon
import xbmcaddon


__addon__       = xbmcaddon.Addon()
__addon__ = xbmcaddon.Addon()
__addonname__   = __addon__.getAddonInfo('name')
__addonname__ = __addon__.getAddonInfo('name')
__icon__       = __addon__.getAddonInfo('icon')
__icon__ = __addon__.getAddonInfo('icon')
   
   
line1 = "This is a simple example of notifications"
line1 = "This is a simple example of notifications"
time = 5000 #in miliseconds
time = 5000 #in miliseconds
   
   
xbmc.executebuiltin('Notification(%s, %s, %d, %s)'%(__addonname__,line1, time, __icon__))
xbmc.executebuiltin('Notification(%s, %s, %d, %s)'%(__addonname__,line1, time, __icon__))
</source>
</source>


===using xbmcgui module===
=== using xbmcgui module ===


This will show a OK dilaog box with the message
This will show a OK dilaog box with the message
Line 47: Line 47:
import xbmcgui
import xbmcgui


__addon__       = xbmcaddon.Addon()
__addon__ = xbmcaddon.Addon()
__addonname__   = __addon__.getAddonInfo('name')
__addonname__ = __addon__.getAddonInfo('name')


line1 = "This is a simple example of OK dialog"
line1 = "This is a simple example of OK dialog"
Line 57: Line 57:
</source>
</source>


==Using JSON-RPC==
== Using JSON-RPC ==




==Using xbmc modules & JSON-RPC API==
== Using xbmc modules & JSON-RPC API ==


More info on JSON-RPC API can be found here: [[JSON-RPC_API]]
More info on JSON-RPC API can be found here: [[JSON-RPC API]]


This JSON-RPC command send through various ways and will execute the addon code that is shown below.
This JSON-RPC command send through various ways and will execute the addon code that is shown below.
Line 76: Line 76:
             "line1": "Hello World",
             "line1": "Hello World",
             "line2": "Showing this message using",
             "line2": "Showing this message using",
             "line3": "Combination of XBMC python modules and",
             "line3": "Combination of Kodi python modules and",
             "line4": "JSON-RPC API interface",
             "line4": "JSON-RPC API interface",
             "line5": "Have fun coding"
             "line5": "Have fun coding"
Line 111: Line 111:
</source>
</source>


=See also=
= See also =
'''Development:'''
'''Development:'''
* [[Addon Settings]]
* [[Addon Settings]]

Revision as of 08:36, 4 May 2015

Home icon grey.png   ▶ Development ▶ Add-on development ▶ Python development ▶ Python examples ▶ GUI tutorial


Introduction

This How To will explain several ways of showing a "Hello World" popup on screen

Examples

Using xbmc modules

Simple example using xbmc python modules showing a notification dialog that will show for 5 seconds.

using build-in functions

More info on Built-In functions can be found here: List of built-in functions We will two examples. One with just a simple notification dialog and the other with the same dialog but with using variables. It all depends on how extended you scripts will be. Since we require all used strings to be localized (translatable) we recommend the second option.

import xbmc

xbmc.executebuiltin('Notification(Hello World,This is a simple example of notifications,5000,/script.hellow.world.png)')


import xbmc
import xbmcaddon

__addon__ = xbmcaddon.Addon()
__addonname__ = __addon__.getAddonInfo('name')
__icon__ = __addon__.getAddonInfo('icon')
 
line1 = "This is a simple example of notifications"
time = 5000 #in miliseconds
 
xbmc.executebuiltin('Notification(%s, %s, %d, %s)'%(__addonname__,line1, time, __icon__))

using xbmcgui module

This will show a OK dilaog box with the message More info on xbmcgui functions can be found here: [ http://mirrors.xbmc.org/docs/python-docs/xbmcgui.html#Dialog ]

import xbmcaddon
import xbmcgui

__addon__ = xbmcaddon.Addon()
__addonname__ = __addon__.getAddonInfo('name')

line1 = "This is a simple example of OK dialog"
line2 = "Showing this message using"
line3 = "XBMC python modules"

xbmcgui.Dialog().ok(__addonname__, line1, line2, line3)

Using JSON-RPC

Using xbmc modules & JSON-RPC API

More info on JSON-RPC API can be found here: JSON-RPC API

This JSON-RPC command send through various ways and will execute the addon code that is shown below.

{
    "jsonrpc": "2.0",
    "id": 0,
    "method": "Addons.ExecuteAddon",
    "params": {
        "addonid": "script.popup",
        "params": {
            "image": "D:\\heartagram.jpg",
            "line1": "Hello World",
            "line2": "Showing this message using",
            "line3": "Combination of Kodi python modules and",
            "line4": "JSON-RPC API interface",
            "line5": "Have fun coding"
        }
    }
}


This python code will create several controls using xbmcgui module and fill these will the message send through JSON-RPC:

import xbmcgui
import sys
import urlparse

class PopupWindow(xbmcgui.WindowDialog):
    def __init__(self, image, line1, line2, line3, line4, line5):
        self.addControl(xbmcgui.ControlImage(x=25, y=25, width=150, height=150, filename=image[0]))
        self.addControl(xbmcgui.ControlLabel(x=190, y=25, width=500, height=25, label=line1[0]))
        self.addControl(xbmcgui.ControlLabel(x=190, y=50, width=500, height=25, label=line2[0]))
        self.addControl(xbmcgui.ControlLabel(x=190, y=75, width=500, height=25, label=line3[0]))
        self.addControl(xbmcgui.ControlLabel(x=190, y=100, width=500, height=25, label=line4[0]))
        self.addControl(xbmcgui.ControlLabel(x=190, y=125, width=500, height=25, label=line5[0]))

if __name__ == '__main__':
    params = urlparse.parse_qs('&'.join(sys.argv[1:]))
    window = PopupWindow(**params)
    window.show()
    xbmc.sleep(5000)
    window.close()
    del window

See also

Development: