Home » Kodi » Python How To Take User Input in Kodi

Python How To Take User Input in Kodi

Python How To Take User Input in Kodi

Continuing on our quest to build a working Kodi addon as part of our Kodi addon development, today we are going to see how to take user input in Kodi.

python how to take user input

Python How To Take User Input in Kodi

Pre-Requisites

Make sure you have read our previous article on sys.argv explained which also tells you how to build menus. In case you haven’t hit the link below.

Kodi Addon Development : sys.argv Explained, Building Menus

Purpose

The purpose of this addon or plugin is to take user input and display it. A video is played when user clicks on the search item. The videos are hardcoded right now, but eventually we can replace them with links we scrape of the internet. Scraping is a different animal all together, so that is not part of this tutorial.

Addon name – plugin.video.s3arch

The code for this addon can be found here, and you can grab the addon zip from here.

Python How to Take User Input in Kodi

Let’s Dive Into the Code

All the code for this addon is present in the file default.py. We will start with the getusersearch function at line 16. Anything before is covered in the how to link in pre-requisites section.

def getusersearch(language):
 kb = xbmc.Keyboard('default', 'heading')
 kb.setDefault('Enter Search Word')
 kb.setHeading(language + 'Search')
 kb.setHiddenInput(False)
 kb.doModal()
 if (kb.isConfirmed()):
 search_term = kb.getText()
 return(search_term)
 else:
 return

 

def getusersearch(language):

Defines a function named getusersearch which takes a parameter as input, we call it language.

kb = xbmc.Keyboard('default', 'heading')

creates a new keyboard object and assigns it to variable kb. xbmc.Keyboard is an inbuilt method in xbmc library. default is the text which will appear in the search text bar, and heading is the heading. We override both of these below.

kb.setDefault('Enter Search Word')

We add the default text in the search bar to ‘Enter Search Word’

kb.setHeading(language + 'Search')

We set the heading to parameter taken as input and add word Search to it.

kb.setHiddenInput(False)

We set hidden input as False, whatever is entered will show on screen. If this is set to True, text entered won’t show on keyboard. Useful when you are entering passwords.

kb.doModal()

This shows the keyboard to the user and waits for input.

if (kb.isConfirmed()):

If user presses the ok button after entering the text. This check ensures that user pressed the ok button. This is because if user enters text and presses cancel button, even then kb.getText() will have what user entered. In short, irrespective of the fact whether user presses Ok or Cancel after entering text you can get his text by using getText()

search_term = kb.getText()

set the variable search_term with what user entered.

 return(search_term)

We return the search_term.

 else:

if use did not press Ok

 return

we return nothing

mode = args.get('mode', None)

covered in pre-requisites link.

Now we are going to build a menu.

if mode is None:
 url = build_url({'mode': 'search', 'language': 'Hindi'})
 li = xbmcgui.ListItem('Hindi Search', iconImage='DefaultFolder.png')
 xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
 listitem=li, isFolder=True)
 url = build_url({'mode': 'search', 'language': 'English'})
 li = xbmcgui.ListItem('English Search', iconImage='DefaultFolder.png')
 xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
 listitem=li, isFolder=True)
 xbmcplugin.endOfDirectory(addon_handle)

The menu will show up when the addon is first started. Below is the image of what we just built.

When you click either Hindi search or English search, the else loop will be executed.

elif mode[0] == 'search':
 language = args['language'][0]
 search_string = getusersearch(language)
 xbmcplugin.setContent(addon_handle, 'movies')
 if language == 'Hindi':
 url = 'http://www.vidsplay.com/wp-content/uploads/2017/04/alligator.mp4'
 li = xbmcgui.ListItem(search_string + ' Video', iconImage='DefaultVideo.png')
 xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)
 xbmcplugin.endOfDirectory(addon_handle)

Most the code here can be comprehended if you understood the post about argv and building menus linked in pre-requisites section. We will look at some of it still.

language = args['language'][0]

so language will either be Hindi or English based on what option was selected.

search_string = getusersearch(language)

Here we are calling function getusersearch with language selected as parameter. search_string will have whatever use wants to search (if he presses Ok after entering his search string).

xbmcplugin.setContent(addon_handle, 'movies')

We change the content mode to movies.

if language == 'Hindi':

if language was hindi we will enter this loop, and create a menu again.

url = 'http://www.vidsplay.com/wp-content/uploads/2017/04/alligator.mp4'
 li = xbmcgui.ListItem(search_string + ' Video', iconImage='DefaultVideo.png')
 xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)
 xbmcplugin.endOfDirectory(addon_handle)

We have the same logic for English part as well.

elif language == 'English':
 url = 'http://www.vidsplay.com/wp-content/uploads/2017/04/alligator.mp4'
 li = xbmcgui.ListItem(search_string + ' Video', iconImage='DefaultVideo.png')
 xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)
 xbmcplugin.endOfDirectory(addon_handle)

Python How to Take User Input in Kodi

Runtime

Let us go ahead and see what part of code is executed during runtime or when we open the addon.

When you open the addon for the first time, below screen is displayed and the code responsible is.

if mode is None:
 url = build_url({'mode': 'search', 'language': 'Hindi'})
 li = xbmcgui.ListItem('Hindi Search', iconImage='DefaultFolder.png')
 xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
 listitem=li, isFolder=True)
 url = build_url({'mode': 'search', 'language': 'English'})
 li = xbmcgui.ListItem('English Search', iconImage='DefaultFolder.png')
 xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
 listitem=li, isFolder=True)
 xbmcplugin.endOfDirectory(addon_handle)

how to take user input - kodi addon development 001

Now, we click on Hindi Search, we will get a keyboard popup asking us for input. The code responsible for this is.

elif mode[0] == 'search':
 language = args['language'][0]
 search_string = getusersearch(language)

take user input - kodi addon development 002

Now say you enter hindi movies in the search, and click on Ok.

take user input - kodi addon development

You will get below screen, and the code responsible is.

if language == 'Hindi':
 url = 'http://www.vidsplay.com/wp-content/uploads/2017/04/alligator.mp4'
 li = xbmcgui.ListItem(search_string + ' Video', iconImage='DefaultVideo.png')
 xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)
 xbmcplugin.endOfDirectory(addon_handle)

take user input - kodi addon development 006

Notice the change in how the screen looks from when you first opened the addon, to when you entered the search word. This is because we set the content as movies.

xbmcplugin.setContent(addon_handle, 'movies')

 

If you select hindi Movie Video, our hardcoded video will play. The next logical step here would be to use the search string to scrape a website and display results to the user.

Python How To Take User Input in Kodi

Bug in Code

We have a bug in our code, wherein if you press Cancel it gives an error notification. We are working on fixing it, but if you find a fix before us let us know via comments below.

References

http://mirrors.kodi.tv/docs/python-docs/14.x-helix/xbmc.html#Keyboard

Do you STREAM on Kodi WITHOUT a VPN?

We STRONGLY suggest you use a VPN Service like IPVanish.

Why VPN? 1. Stay Secure and Anonymous Online 2. Stream Content Anonymously 3. Access Geo Locked Content 4. Hide Your Activity from your ISP

Why IPVanish? 1. They Keep No Logs = No Tracking 2. Native apps for Android, Android TV, iOS, Mac, Linux 3. No Speed Slowdown 4. Highly Recommended 5. 7 Days Money Back Guarantee

Sign-Up for IPVanish

Follow Us

Close
Please support the site
By clicking any of these buttons you help our site to get better