基维,弱引用对象不再存在



这是我的kv文件

<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

Python 代码中的一些逻辑和动态内容

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]

当我按下具有 id ->刷新(以 KV 定义)的按钮时,它工作正常,但在第三次出现错误:

线程

线程 1 中的异常:回溯(最近最后一次调用):
文件 "/usr/lib/python3.5/threading.py",第 914 行,_bootstrap_inner self.run() 文件 "/usr/lib/python3.5/threading.py",第 862 行,运行中 self._target(*self._args, **self._kwargs) 文件 "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/input/providers/mtdev.py", 197行,_thread_run _device = Device(_fn) File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/lib/mtdev.py", 第 131 行,在initself._fd = os.open(文件名,操作系统。O_NONBLOCK |操作系统。O_RDONLY) 权限错误: [错误 13] 权限被拒绝: '/dev/input/event6'animation
= Animation(ang=360) animation.start(self.ids.refresh) animation.bind(on_complete=self.clear) 動畫結束。 动画 = Animation(ang=360) animation.start(self.ids.refresh) animation.bind(on_complete=self.clear) 動畫結束。 [资讯][基地
] 正在申请中... 回溯(最近一次调用) 最后):文件"main.py",第 146 行,在 AccountMaintainerApp().run() File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/app.py", line 824,运行中 runTouchApp() 文件 "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/base.py", 第 487 行,在运行触摸应用程序中 EventLoop.window.mainloop() File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/core/window/window_sdl2.py", 525 号线,在主环路 self._mainloop() 文件 "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/core/window/window_sdl2.py", 290行,_mainloop EventLoop.idle() File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/base.py", 第 327 行,处于空闲状态 Clock.tick() 文件 "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/clock.py", 第 483 行,在刻度中 self._process_events() 文件 "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/clock.py", 615行,_process_events event.tick(self._last_tick, remove) 文件 "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/clock.py", 第 374 行,在刻度中 ret = callback(self._dt) File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/animation.py", 342号线,_update self.stop(widget) 文件 "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/animation.py", 213 路,在站 self.cancel(widget) File "/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/animation.py", 第 222 行,在取消中 self._widgets.pop(widget.uid, None) 文件 "kivy/weakproxy.pyx", 第 19 行, in kivy.weakproxy.WeakProxy.getattr(/tmp/pip-build-zy75v30v/kivy/kivy/weakproxy.c:1097) File "kivy/weakproxy.pyx",第 15 行,在 kivy.weakproxy.WeakProxy 中。ref(/tmp/pip-build-zy75v30v/kivy/kivy/weakproxy.c:1004) ReferenceError: 弱引用对象不再存在

编程指南 » Kv 语言

id 是对小部件的弱引用,而不是小部件本身。因此,存储 id 不足以防止小部件被垃圾回收。

要使小部件保持活动状态,必须保留对 id刷新的直接引用。在这种情况下,这是使用id.__self__refresh.__self__来实现的。

KV文件

<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

最新更新