Home > OS >  python code for kivy app throws the error: KeyError: ''
python code for kivy app throws the error: KeyError: ''

Time:12-17

I'm trying to run a kivy app but i keep getting a (KeyError: '') error i tried different things but couldn't get it to work. p.s : dic is my local file that contains: -dic_trans- and -full- as very long dictionaries.

using a counter variable x to increment the index of the key-value, of both of the dictionaries such as:

when clicked on the first button(yes), the first label should receive the key of the first dictionary, and the second label should receive the value of that key

Similarly for the second button(no) the first label should receive the key of the second dictionary, and the second label should receive the value of that key

i am not sure where I have missed out, it might be due to some mistake in using properties, or something else, i am a bit confused since I'm kinda new to kivy..

the python code is as follows:

    from dic import dic_trans
    from dic import full
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.properties import StringProperty
    from kivy.properties import  NumericProperty
    from kivy.properties import DictProperty
    class MainBoxLayout (BoxLayout):
        label1property = StringProperty()
        label2property = StringProperty()
        x = NumericProperty(-1)
        y = DictProperty(dic_trans)
        z = DictProperty(full)
        def press_yes(self):
            global x
            self.x  = 1
            self.label1property = str(self.y[self.x])
        def press_no(self):
            self.label2property = str(self.z[self.x])
        class BasicWordsPracticeApp(App):
            pass
    BasicWordsPracticeApp().run()

and for the kv file:

    MainBoxLayout:
    <MainBoxLayout>:
        padding:("7dp","7dp","7dp","7dp")
        orientation: "vertical"
        Label:
            text: root.label1property
            multiline: True
            font_size: 50
            background_color: (0,0,1,1)
            canvas.before:
                Color:
                    rgba: self.background_color
                Rectangle:
                    size: self.size
                    pos: self.pos
        Label:
            text: root.label2property
            multiline: True
            size_hint:  1,None
            height: "450dp"
            font_size: 50
            background_color: (1,1,0,1)
            canvas.before:
                Color:
                    rgba: self.background_color
                Rectangle:
                    size: self.size
                    pos: self.pos
        BoxLayout:
            Button:
                text: "No"
                on_press: root.press_no()
                background_color: (255,0,0,1)

            Button:
                text: "Yes"
                on_press: root.press_yes()
                background_color: (0,255,0,1)

   

the error is as follows: . . .

    [INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil, 
    img_ffpyplayer 
     Traceback (most recent call last):
       File "hope.py", line 38, in <module>
         BasicWordsPracticeApp().run()
       File "/", line 949, 
    in run
          self._run_prepare()
       File "/", line 918, 
    in _run_prepare
         self.load_kv(filename=self.kv_file)
       File "/", line 691, 
    in load_kv
         root = Builder.load_file(rfilename)
       File "/", 
    line 306, in load_file
         return self.load_string(data, **kwargs)
       File "/", 
    line 404, in load_string
         widget = Factory.get(parser.root.name)(__no_builder=True)
       File "kivy/_event.pyx", line 195, in kivy._event.EventDispatcher.__cinit__
       File "kivy/properties.pyx", line 1317, in 
    kivy.properties.ReferenceListProperty.link_deps
       File "kivy/properties.pyx", line 465, in kivy.properties.Property.fbind
     KeyError: ''

CodePudding user response:

Your issue is that you are using letters like "x", "y", "z", the same error replicates for me as well. try using the following instead:

x_num = NumericProperty(-1)
y_dict = DictProperty(dic_trans)
z_dict = DictProperty(full)

I believe it might be that x y and z are default variables for kivy.

Regarding the labels:

You can control the labels in a different way, by giving them ids. you need to give the label and ID, and then reference it in the parent, in order to be able to access it in any of the parents functions. try this instead:

.py

from dic import dic_trans
from dic import full
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.properties import NumericProperty
from kivy.properties import DictProperty


class BasicWordsPracticeApp(App):
    pass


class MainBoxLayout(BoxLayout):
    x_num = NumericProperty(-1)
    y_dict = DictProperty(dic_trans)
    z_dict = DictProperty(full)

    def press_yes(self):
        global x
        self.x  = 1
        # accessing the .text variable of the label id label_1
        self.label_1.text = str(self.y[self.x])
       
    def press_no(self):
        # accessing the .text variable of the label id label_2
        self.label_2.text = str(self.z[self.x])
     

BasicWordsPracticeApp().run()

.kv

MainBoxLayout:

<MainBoxLayout>
    label_1: label_1
    label_2: label_2
    padding:("7dp","7dp","7dp","7dp")
    orientation: "vertical"
    Label:
        id: label_1
        text: "some default value"
        multiline: True
        font_size: 50
        background_color: (0,0,1,1)
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos
    Label:
        id: label_2
        text: "some default value"
        multiline: True
        size_hint:  1,None
        height: "450dp"
        font_size: 50
        background_color: (1,1,0,1)
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos
    BoxLayout:
        Button:
            text: "No"
            on_press: root.press_no()
            background_color: (255,0,0,1)

        Button:
            text: "Yes"
            on_press: root.press_yes()
            background_color: (0,255,0,1)

CodePudding user response:

@Jonathan: changing names seemed to help proceed to the next step, the kivy app is being opened, , but when clicking on either buttons, I receive a keyError of 0 for clicking on one button, and keyError of -1 for clicking on the other button. to note that I have tried the other method as well

  • Related