有没有人能给点建议?
我有一个应用程序在Pi3/ArchLinuxArm/Kivy1.11上运行得很好。
我正在尝试将应用程序移动到Pi4/RasPiOS Lite (Buster)/Kivy2.0.0。现在,我动态生成按钮的一段代码失败了,错误代码是:
Traceback (most recent call last):
File "Main.py", line 57, in <module>
main()
File "Main.py", line 34, in main
Display.Display()
File "/home/automate/Display.py", line 657, in Display
DisplayApp().run()
File "/home/automate/.local/lib/python3.7/site-packages/kivy/app.py", line 949, in run
self._run_prepare()
File "/home/automate/.local/lib/python3.7/site-packages/kivy/app.py", line 919, in _run_prepare
root = self.build()
File "/home/automate/Display.py", line 649, in build
sm.add_widget(ControlLightsScreen(name='c_lights'))
File "/home/automate/Display.py", line 315, in __init__
on_press = self.on_event
File "/home/automate/.local/lib/python3.7/site-packages/kivy/uix/behaviors/button.py", line 121, in __init__
super(ButtonBehavior, self).__init__(**kwargs)
File "/home/automate/.local/lib/python3.7/site-packages/kivy/uix/label.py", line 318, in __init__
super(Label, self).__init__(**kwargs)
File "/home/automate/.local/lib/python3.7/site-packages/kivy/uix/widget.py", line 350, in __init__
super(Widget, self).__init__(**kwargs)
File "kivy/_event.pyx", line 245, in kivy._event.EventDispatcher.__init__
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
有问题的代码段是:
for j in range(len(Lights.lights)):
i = j
button = Button(
text = Lights.lights[i][2], font_size = 24,
text_size = (160,60),
halign = 'center', valign = 'center',
size_hint_x = None, width = 188,
size_hint_y = None, height = 68,
id=str(i),
color = black,
background_normal = '',
background_color = grey,
on_press = self.on_event
)
self.mylights.append([grey,button])
grid.add_widget(button)
我尝试删除报告错误的行("on_press = self.on_event"),所有这些操作都是将报告的错误移到上面的行。注释掉整段代码后,应用程序就可以正常运行了。
类似的代码,动态生成标签仍然是完全有效的。
我做错了什么吗?还是在1.11.1和2.0.0之间有一些我需要考虑的变化?
启动诊断程序包括:
[INFO ] [Logger ] Record log in /home/automate/.kivy/logs/kivy_21-02-04_10.txt
[INFO ] [Kivy ] v2.0.0
[INFO ] [Kivy ] Installed at "/home/automate/.local/lib/python3.7/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0]
[INFO ] [Python ] Interpreter at "/usr/bin/python"
[INFO ] [Factory ] 186 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] Backend used <sdl2>
[INFO ] [GL ] OpenGL version <b'OpenGL ES 3.1 Mesa 19.3.2'>
[INFO ] [GL ] OpenGL vendor <b'VMware, Inc.'>
[INFO ] [GL ] OpenGL renderer <b'llvmpipe (LLVM 9.0.1, 128 bits)'>
[INFO ] [GL ] OpenGL parsed version: 3, 1
[INFO ] [GL ] Shading version <b'OpenGL ES GLSL ES 3.10'>
[INFO ] [GL ] Texture max size <8192>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
发布于 2021-02-05 16:08:49
我被可靠地告知'id‘不是一个按钮属性,尽管在v2.0.0之前它的行为是这样的
当然,解决方案很简单。从按钮的已分配属性列表中删除id,但在按钮代码button.id = i
调用的正下方添加新行。然后可以在on_event代码中引用obj.id:
for j in range(len(Lights.lights)):
i = j
button = Button(
text = Lights.lights[i][2], font_size = 24,
text_size = (160,60),
halign = 'center', valign = 'center',
size_hint_x = None, width = 188,
size_hint_y = None, height = 68,
color = black,
background_normal = '',
background_color = grey,
on_press = self.on_event
)
button.id = i
self.mylights.append([grey,button])
grid.add_widget(button)
def on_event(self, obj):
myid = obj.id
https://stackoverflow.com/questions/66044110
复制