Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom event : TypeError: 'NoneType' object is not subscriptable

Tags:

python

kivy

I use Kivy 1.8.0 and I try to use the custom event to manage a yesNo popup. This code was found on the internet. I have find only this sample and I want to modify this code to make multiple popup management but I have error when run it.

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.popup import Popup
from kivy.properties import StringProperty

Builder.load_string('''
<ConfirmPopup>:
    cols: 1
        Label:
            text: root.text
        GridLayout:
            cols: 2
            size_hint_y: None
            height: '44sp'
            Button:
                text: 'Yes'
                on_release: root.dispatch('on_reponse_user','yes')
            Button:
                text: 'No'
                on_release: root.dispatch('on_reponse_user', 'no')
''')

I don't know if the problem is in the string for kv or is in the code.

class ConfirmPopup(GridLayout):
    text = StringProperty('')

    def __init__(self,**kwargs):
        self.register_event_type('on_reponse_user')
        super(ConfirmPopup,self).__init__(**kwargs)

    def on_reponse_user(self, instance, answer):
        pass    


class PopupTest(App):
    def build(self):
        content = ConfirmPopup(text='Do You Love Kivy?')
        content.bind(on_reponse_user=self.on_answer_callback)
        self.popup = Popup(title="Answer Question",
                           content=content,
                           size_hint=(None, None),
                           size=(480,400),
                           auto_dismiss= False)
        self.popup.open()

    def on_answer_callback(self, instance, answer):
        print("USER ANSWER: " , repr(answer))
        self.popup.dismiss()

if __name__ == '__main__':
    PopupTest().run()

When I run this code I have This error :

 Traceback (most recent call last):
   File "F:\Kivy-1.8.0\test_event.py", line 24, in <module>
     ''')
   File "F:\Kivy-1.8.0\kivy\kivy\lang.py", line 1491, in load_string
     parser = Parser(content=string, filename=fn)
   File "F:\Kivy-1.8.0\kivy\kivy\lang.py", line 1049, in __init__
     self.parse(content)
   File "F:\Kivy-1.8.0\kivy\kivy\lang.py", line 1122, in parse
     objects, remaining_lines = self.parse_level(0, lines)
   File "F:\Kivy-1.8.0\kivy\kivy\lang.py", line 1271, in parse_level
     if current_property[:3] == 'on_':
 TypeError: 'NoneType' object is not subscriptable

I don't know why I have an error with the custom event. Like the documentation in Kivy it is possible to add custom event.

like image 305
Serial86 Avatar asked May 16 '14 12:05

Serial86


1 Answers

The error is thrown in the kivy parser for sure, which suggests the error is in the string you're trying to load. It looks like an indentation issue in the string.

If the example has been taken from here there's something funky about the indentation level mixing in that snippet that the GitHub code formatter might be hiding. If your indentation is like on that page, try copying the code snippet you've posted exactly as it's formatted here, and see if that fixes things.

like image 62
dabhaid Avatar answered Sep 29 '22 11:09

dabhaid