使用Kivy创建按钮时,是否有“TypeError:object.\uuuu init\uuuuu()不接受任何参数”的修复程序?

2024-06-16 13:03:45 发布

您现在位置:Python中文网/ 问答频道 /正文

当我以前使用按钮时,将**kwargs传递给object.__init__()效果很好。在我的网格中添加一个按钮后,它现在显示此错误

[INFO   ] [Logger      ] Record log in C:\Users\trist\.kivy\logs\kivy_20-10-15_0.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.gstreamer" 0.3.1
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.3.1
[INFO   ] [Kivy        ] v1.11.1
[INFO   ] [Kivy        ] Installed at "C:\Users\trist\PycharmProjects\AndroisOS\venv\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)]
[INFO   ] [Python      ] Interpreter at "C:\Users\trist\PycharmProjects\AndroisOS\venv\Scripts\python.exe"
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.6.0 - Build 26.20.100.7263'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) HD Graphics 530'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 - Build 26.20.100.7263'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
Traceback (most recent call last):
  File "C:/Users/trist/PycharmProjects/AndroisOS/main.py", line 47, in <module>
    MyApp().run()
  File "C:\Users\trist\PycharmProjects\AndroisOS\venv\lib\site-packages\kivy\app.py", line 829, in run
    root = self.build()
  File "C:/Users/trist/PycharmProjects/AndroisOS/main.py", line 38, in build
    return MyGrid()
  File "C:/Users/trist/PycharmProjects/AndroisOS/main.py", line 32, in __init__
    self.submit = Button(text="Next", fontsize=40)
  File "C:\Users\trist\PycharmProjects\AndroisOS\venv\lib\site-packages\kivy\uix\behaviors\button.py", line 121, in __init__
    super(ButtonBehavior, self).__init__(**kwargs)
  File "C:\Users\trist\PycharmProjects\AndroisOS\venv\lib\site-packages\kivy\uix\label.py", line 318, in __init__
    super(Label, self).__init__(**kwargs)
  File "C:\Users\trist\PycharmProjects\AndroisOS\venv\lib\site-packages\kivy\uix\widget.py", line 350, in __init__
    super(Widget, self).__init__(**kwargs)
  File "kivy\_event.pyx", line 243, in kivy._event.EventDispatcher.__init__
TypeError: object.__init__() takes no arguments

Process finished with exit code 1

我从文件中得到这个错误,看起来是这样的(我的问题只发生在我使用按钮时):


from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button

class MyGrid(GridLayout):
    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 1

self.add_widget(Label(text="What is your EAVE/PURLIN width?"))
self.metal = TextInput(multiline=False)
self.add_widget(self.metal)
        
self.submit = Button(text="Next", fontsize=40)
self.add_widget(self.submit)

class MyApp(App):
    def build(self):
        return MyGrid()

if __name__ == "__main__":
    MyApp().run()


Tags: depsinpyselfinfoinitlineusers
1条回答
网友
1楼 · 发布于 2024-06-16 13:03:45

看起来您将错误的参数传递给了Button构造函数。它希望字体大小由^{}参数指定,但您使用的fontsize不带下划线。尝试:

self.submit = Button(text="Next", font_size=40)

通常,当您使用不正确的参数调用函数时,您会得到一条更有用的错误消息。在这种情况下,Kivy的错误信息是如此无用,这是Kivy的一个小缺点

相关问题 更多 >