0
votes

This is my kv file

<GetAccount>:
    Button:
        id: refresh
        ang: 0
        background_normal: 'static/refresh.png'
        size_hint: None, None
        size: sp(50), sp(50)
        pos_hint: {'center_y': .96, 'center_x': .8} 
        on_press: root.clean()
        canvas.before:
            Rotate:
                angle: self.ang
                origin: self.center

some logic and dynamic contents in python code

class GetAccount(FloatLayout):

    keyandnick = ObjectProperty(KeyAndNick())
    account = ObjectProperty(Account())

    def __init__(self, **kargs):
        super(GetAccount, self).__init__(**kargs)
        layout = StackLayout(size_hint_y=None)
        layout.bind(minimum_height=layout.setter('height'))
        nicks = self.keyandnick.get_nicks()
        for nick in nicks:
            layout.add_widget(Button(text=str(nick[0]), size_hint_y=None, height=sp(60), on_press=self.ask_for_key))
        scroll = ScrollView(size_hint=(1, .9), bar_width=sp(5), bar_inactive_color=[.7, .7, .7, .5])
        scroll.add_widget(layout)
        self.add_widget(scroll)

    def ask_for_key(self, obj):
        self.clear_widgets()
        self.account_name = obj.text
        boxlayout = BoxLayout(size_hint=(1, .4), orientation='vertical', pos_hint={'center_y': .5})
        self.key = TextInput(pos_hint={'center_y': .5}, hint_text='Key', font_size=sp(30), password=True)
        boxlayout.add_widget(self.key)
        boxlayout.add_widget(Button(text='Get Account', font_size=sp(30), on_press=self.get_account))
        boxlayout.add_widget(Button(text='Delete Account', pos_hint={'center_x': .5}, font_size=sp(30), on_press=self.delete_account))
        popup = Popup(title=self.account_name, content=boxlayout, size_hint=(1, .7))
        popup.bind(on_dismiss=self.clear)
        popup.open()

    def get_account(self, obj):
        if self.keyandnick.check_key(self.key.text):
            self.clear_widgets()
            boxlayout = BoxLayout(orientation='vertical', size_hint=(1, .6), pos_hint={'center_y': .5})
            _account = self.account.get_account(self.account_name)
            a = True
            for data in _account:
                if a:
                    boxlayout.add_widget(Label(text=data, font_size=sp(30), bold=True, underline=True))
                    a = False
                else:
                    boxlayout.add_widget(Label(text=data, font_size=sp(20)))
            popup = Popup(title=self.account_name, content=boxlayout, size_hint=(1, .7))
            popup.open()
        else:
            self.key.background_color = [0, 0, 255, 0.3]

    def clean(self):
        animation = Animation(ang=360)
        animation.start(self.ids.refresh)
        animation.bind(on_complete=self.clear)

    def clear(self, a, b):
        self.clear_widgets()
        self.add_widget(GetAccount())

    def delete_account(self, obj):
        if self.keyandnick.check_key(self.key.text):
            self.account.delete_account(self.account_name)
            self.keyandnick.del_nick(self.account_name)
        else:
            self.key.background_color = [0, 0, 255, 0.3]

when i pressed button which has id -> refresh (defined in kv) it works fine but in third time it gives error:

Exception in thread Thread-1: Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/input/providers/mtdev.py", line 197, in _thread_run _device = Device(_fn) File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/lib/mtdev.py", line 131, in init self._fd = os.open(filename, os.O_NONBLOCK | os.O_RDONLY) PermissionError: [Errno 13] Permission denied: '/dev/input/event6'
animation = Animation(ang=360) animation.start(self.ids.refresh) animation.bind(on_complete=self.clear) animation end. animation = Animation(ang=360) animation.start(self.ids.refresh) animation.bind(on_complete=self.clear) animation end. [INFO ] [Base
] Leaving application in progress... Traceback (most recent call last): File "main.py", line 146, in AccountMaintainerApp().run() File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/app.py", line 824, in run runTouchApp() File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/base.py", line 487, in runTouchApp EventLoop.window.mainloop() File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/core/window/window_sdl2.py", line 525, in mainloop self._mainloop() File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/core/window/window_sdl2.py", line 290, in _mainloop EventLoop.idle() File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/base.py", line 327, in idle Clock.tick() File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/clock.py", line 483, in tick self._process_events() File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/clock.py", line 615, in _process_events event.tick(self._last_tick, remove) File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/clock.py", line 374, in tick ret = callback(self._dt) File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/animation.py", line 342, in _update self.stop(widget) File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/animation.py", line 213, in stop self.cancel(widget) File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/animation.py", line 222, in cancel self._widgets.pop(widget.uid, None) File "kivy/weakproxy.pyx", line 19, in kivy.weakproxy.WeakProxy.getattr (/tmp/pip-build-zy75v30v/kivy/kivy/weakproxy.c:1097) File "kivy/weakproxy.pyx", line 15, in kivy.weakproxy.WeakProxy.ref (/tmp/pip-build-zy75v30v/kivy/kivy/weakproxy.c:1004) ReferenceError: weakly-referenced object no longer exists

1

1 Answers

2
votes

Programming Guide » Kv language

An id is a weakref to the widget and not the widget itself. As a consequence, storing the id is not sufficient to keep the widget from being garbage collected.

To keep the widget alive, a direct reference to the id refresh must be kept. This is achieved using id.__self__ or refresh.__self__ in this case.

kv file

<GetAccount>:
    refresh: refresh.__self__
    Button:
        id: refresh
        ang: 0
        background_normal: 'static/refresh.png'
        size_hint: None, None
        size: sp(50), sp(50)
        pos_hint: {'center_y': .96, 'center_x': .8}
        on_press: root.clean()
        canvas.before:
            Rotate:
                angle: self.ang
                origin: self.center