我有一个CustomDropDown小部件网格,每次都不能以相同的方式工作。但每次我都会遇到这个常见错误:
ReferenceError: weakly-referenced object no longer exists
我的python代码如下:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
Mess = {
'mess1': 'RR - SI',
'mess2': 'VTH - SI',
'mess3': 'SK - SI',
'mess4': 'CV - SI',
'mess5': 'CR - NI',
'mess6': 'AD - NI',
'mess7': 'MD - NI',
'mess8': 'MDJ - NI',
'mess9': 'FC - HM'
}
Builder.load_string('''
<ChoiceLayout>
orientation:'vertical'
Label:
text: "Mess Registration for Jul-Nov 2016"
size_hint: (None,None)
font_size:'48dp'
size_hint_y:1
pos_hint: {'center_x': .5}
BoxLayout:
orientation:'horizontal'
size_hint_y:2
Label:
text: 'NAME : {}'.format('MyName')
size_hint: (None,None)
font_size:'36dp'
pos_hint: {'left': 1}
Label:
text: 'ROLL NUMBER : {}'.format('CS22A565')
size_hint: (None,None)
font_size:'36dp'
pos_hint: {'right': 1}
BoxLayout:
orientation:'vertical'
size_hint_y: 4
BoxLayout:
orientation:'horizontal'
CustomDropDown:
id: dropdown1
CustomDropDown:
id: dropdown2
CustomDropDown:
id: dropdown3
BoxLayout:
orientation:'horizontal'
CustomDropDown:
id: dropdown4
CustomDropDown:
id: dropdown5
CustomDropDown:
id: dropdown6
BoxLayout:
orientation:'horizontal'
CustomDropDown:
id: dropdown7
CustomDropDown:
id: dropdown8
CustomDropDown:
id: dropdown9
Button:
text: 'Submit'
<CustomDropDown>
orientation:'vertical'
Button:
id: btn
text:'click me'
on_release: dropdown.open(self)
size_hint_y: None
height: '24dp'
DropDown:
id: dropdown
on_parent: self.dismiss()
on_select: btn.text= '{}'.format(args[1])
size_hint_y: None
height: '24dp'
''')
class ChoiceLayout(BoxLayout):
pass
class CustomDropDown(BoxLayout):
def build(self):
tempMess = Mess
for mess,mname in tempMess:
mBtn = Button(text = mname,on_release = self.dropdown.select(mess))
self.dropdown.add_widget(mBtn)
del tempMess[mess]
def selected(args):
self.ids['btn'].text='{}'.format(args)
class RegisterApp(App):
def build(self):
return ChoiceLayout()
if __name__ == '__main__':
RegisterApp().run()
所以,我可能想做的事情是:
控制台中的日志是:
Traceback (most recent call last):
File "prog.py", line 100, in <module>
RegisterApp().run()
File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 828, in run
runTouchApp()
File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 487, in runTouchApp
EventLoop.window.mainloop()
File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 619, in mainloop
self._mainloop()
File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 362, in _mainloop
EventLoop.idle()
File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 330, in idle
self.dispatch_input()
File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 315, in dispatch_input
post_dispatch_input(*pop(0))
File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 281, in post_dispatch_input
wid.dispatch('on_touch_up', me)
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7699)
File "/usr/lib/python2.7/dist-packages/kivy/uix/behaviors/button.py", line 138, in on_touch_up
self.dispatch('on_release')
File "kivy/_event.pyx", line 714, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7654)
File "kivy/_event.pyx", line 1224, in kivy._event.EventObservers.dispatch (kivy/_event.c:13497)
File "kivy/_event.pyx", line 1108, in kivy._event.EventObservers._dispatch (kivy/_event.c:12329)
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1557, in custom_callback
exec(__kvlang__.co_value, idmap)
File "<string>", line 57, in <module>
File "kivy/weakproxy.pyx", line 19, in kivy.weakproxy.WeakProxy.__getattr__ (kivy/weakproxy.c:1143)
File "kivy/weakproxy.pyx", line 15, in kivy.weakproxy.WeakProxy.__ref__ (kivy/weakproxy.c:1042)
ReferenceError: weakly-referenced object no longer exists