Solved How to handle empty "Edit"
#1
Hi!

I'm pretty new to programming and I'm working on an addon.
My addon processes the user input from several "Edit"-s (xbmcgui.ControlEdit), and it has to make sure, none of the "Edit"-s are empty, before calling a function.

I've tried it two ways, both of them were working, but which is the right way to implement it?

The addon should notify the user and terminate the execution without closing the window, so the user can try again.

First I've tried it without catching the Exception:
PHP Code:
import xbmcgui

dialog 
xbmcgui.Dialog()

class 
EmptyEditException(Exception):
    
def __init__(self):
        
msg "Some edits are empty."
        
xbmc.log(msg)
        
dialog.notification("Alert"msg)

def check_if_empty(s):
    if 
== "":
        
raise EmptyEditException

edit0 
xbmcgui.ControlEdit (10025012575'Status')

def some_function():
    
input edit0.getText()
    
check_if_empty(input)
    
some_other_func(input

So the execution will be terminated, because of the exception.

The other way:
PHP Code:
import xbmcgui

dialog 
xbmcgui.Dialog()

class 
EmptyEditException(Exception):
    
def __init__(self):
        
msg "Some edits are empty."
        
xbmc.log(msg)

def check_if_empty(s):
    if 
== "":
        
raise EmptyEditException

edit0 
xbmcgui.ControlEdit (10025012575'Status')

def some_function():
    
input edit0.getText()
    
check_if_empty(input)
    
some_other_func(input)

def button_handler():
    try:
        
some_function()
    
except EmptyEditException:
        
dialog.notification("Alert""Fill all the edits!"

How would you implement it?

Thanks!
Reply
#2
if you use doModal() to show your window, it won't auto-close when the script ends.
your addon will have to explicitly call close() in order to close the window.

http://mirrors.xbmc.org/docs/python-docs...og-doModal
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#3
(2016-12-24, 02:07)ronie Wrote: if you use doModal() to show your window, it won't auto-close when the script ends.
your addon will have to explicitly call close() in order to close the window.

http://mirrors.xbmc.org/docs/python-docs...og-doModal

Thanks for the reply!

I do use doModal() (maybe I should have posted the full code), both of my examples work, I only want to know, which one is the right, or the better way to do it.

The first one lets the Exception happen, the second one catches it and creates a notification.
Reply
#4
i think i would do it like this (as i see no need for try / except / raise)

Code:
def some_function():
    input = edit0.getText()
    if input:
        some_other_func(input)
        return True

def button_handler():
    succes = some_function()
    if not success:
        dialog.notification("Alert", "Fill all the edits!")
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#5
(2016-12-24, 02:49)ronie Wrote: i think i would do it like this (as i see no need for try / except / raise)

Code:
def some_function():
    input = edit0.getText()
    if input:
        some_other_func(input)
        return True

def button_handler():
    succes = some_function()
    if not success:
        dialog.notification("Alert", "Fill all the edits!")

Thank you so much!
This is way more simple than my solution.
Reply

Logout Mark Read Team Forum Stats Members Help
How to handle empty "Edit"0