这是我的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 - &gt;的按钮时刷新(在kv中定义)它工作正常,但在第三次它给出错误:
线程中的异常Thread-1:Traceback(最近一次调用最后一次):
在_bootstrap_inner中输入文件“/usr/lib/python3.5/threading.py”,第914行 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行,在 init 中 self._fd = os.open(filename,os.O_NONBLOCK | os.O_RDONLY)PermissionError:[Errno 13]权限被拒绝:'/ dev / input / event6'
animation =动画(ang = 360)animation.start(self.ids.refresh) animation.bind(on_complete = self.clear)动画结束。 动画= 动画(ang = 360)animation.start(self.ids.refresh) animation.bind(on_complete = self.clear)动画结束。 [INFO] [基础
]离开申请...追溯(最近的电话 last):文件“main.py”,第146行,in AccountMaintainerApp()。run()文件“/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/app.py”,行 824,在奔跑 runTouchApp()文件“/home/ubuntu/.py-3.5.2/lib/python3.5/site-packages/kivy/base.py”, 第487行,在runTouchApp中 EventLoop.window.mainloop()文件“/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()文件“/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)文件“/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)文件“/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行,位于kivy.weakproxy.WeakProxy。 getattr (/tmp/pip-build-zy75v30v/kivy/kivy/weakproxy.c:1097)档案 “kivy / weakproxy.pyx”,第15行,位于kivy.weakproxy.WeakProxy。参考 (/tmp/pip-build-zy75v30v/kivy/kivy/weakproxy.c:1004)ReferenceError: 弱引用对象不再存在
答案 0 :(得分:0)
Programming Guide » Kv language
id是小部件的弱参数,而不是小部件本身。因此,存储id不足以防止小部件被垃圾回收。
要使窗口小部件保持活动状态,必须保留对id refresh 的直接引用。在这种情况下,可以使用 id .__ self __ 或 refresh .__ self __ 来实现。
<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