类型错误:object.__init__() 在将 Kivy 应用程序移动到 2.0.0 时只需要一个参数

问题描述

有人可以提供建议吗?

我有一个应用程序在 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

解决方法

我被可靠地告知 '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