HOW-TO:HelloWorld addon: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
m (Robot: Changing Category:Addon Development to Category:Add-on development)
No edit summary
Line 3: Line 3:


=Introduction=
=Introduction=
This tutorial will explain how to write your first Kodi/XBMC Add-on!


This How To will explain several ways of showing a "Hello World" popup on screen
=Tools=
I'd advise using a text editor for your first add-on, something like notepad++ or sublime are good due to their text syntaxing.
Also make sure you have a version of Kodi installed on your development environment.


=Examples=
=Installing=
For this example we can install the official "Hello World" add-on that is in the repository. It is under:
System >> Add-Ons >> Get Add-Ons >> All Add-ons >> Program Add-Ons


==Using xbmc modules==
=Testing=
You can first give the add-on a test run by going to:
System >> Add-Ons >> Enabled Add-Ons >> Program Add-Ons >> Hello World. You should now see a popup with 3 lines of text.


Simple example using xbmc python modules showing a notification dialog that will show for 5 seconds.
=Modifying the Add-On=
Each time we want to change the add-on you can simply go to the appdata folder and live edit it! No need to even close Kodi. This will allow you to quickly test your add-on modifications.
On windows this will be in:
C:\Users\user\AppData\Roaming\XBMC\addons\plugin.program.hello.world


===using build-in functions===
=Structure=
More info on Built-In functions can be found here: [[List_of_built-in_functions]]
addon.py <-- This is the actual python code for your Add-On
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.


<source lang="python">
addon.xml <-- This is the Add-Ons metadata
import xbmc


xbmc.executebuiltin('Notification(Hello World,This is a simple example of notifications,5000,/script.hellow.world.png)')
changelog.txt <-- This is a text file with any changelog information in it. We advise updating this on each release.
</source>


icon.png <-- A PNG icon for the add-on. It can be 256x256 or 512x512 pixels big. Try to make it look nice!


<source lang="python">
LICENSE.txt <-- Another text file with the Add-On license text.
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__))
</source>
===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 ]
<source lang="python">
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)
</source>
==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.
<source lang="xml">
{
    "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 XBMC python modules and",
            "line4": "JSON-RPC API interface",
            "line5": "Have fun coding"
        }
    }
}
</source>
This python code will create several controls using xbmcgui module and fill these will the message send through JSON-RPC:
<source lang="python">
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
</source>
=See also=
'''Development:'''
* [[Addon Settings]]
* [[Python development]]
* [[Skinning]]


[[Category:Add-on development]]
[[Category:Add-on development]]

Revision as of 13:24, 27 November 2014

Home icon grey.png   ▶ Development ▶ Add-on development ▶ Python development ▶ Python examples ▶ HOW-TO:HelloWorld addon


Introduction

This tutorial will explain how to write your first Kodi/XBMC Add-on!

Tools

I'd advise using a text editor for your first add-on, something like notepad++ or sublime are good due to their text syntaxing. Also make sure you have a version of Kodi installed on your development environment.

Installing

For this example we can install the official "Hello World" add-on that is in the repository. It is under: System >> Add-Ons >> Get Add-Ons >> All Add-ons >> Program Add-Ons

Testing

You can first give the add-on a test run by going to: System >> Add-Ons >> Enabled Add-Ons >> Program Add-Ons >> Hello World. You should now see a popup with 3 lines of text.

Modifying the Add-On

Each time we want to change the add-on you can simply go to the appdata folder and live edit it! No need to even close Kodi. This will allow you to quickly test your add-on modifications. On windows this will be in: C:\Users\user\AppData\Roaming\XBMC\addons\plugin.program.hello.world

Structure

addon.py <-- This is the actual python code for your Add-On

addon.xml <-- This is the Add-Ons metadata

changelog.txt <-- This is a text file with any changelog information in it. We advise updating this on each release.

icon.png <-- A PNG icon for the add-on. It can be 256x256 or 512x512 pixels big. Try to make it look nice!

LICENSE.txt <-- Another text file with the Add-On license text.