python - Kivy: TypeError: object.__init__() 只接受一个参数(要初始化的实例)
问题描述
我一直在尝试修复这个错误,当我尝试将小部件添加到继承 Gridlayout 的类时,我真的不知道继承小部件的类是问题还是 gridlayout 问题,我一直在尝试做很多事情都没有结果,所以我想问一下这是否有解决方案
第一个类是继承小部件的类
class MyPaintElement(Widget):
def __init__(self, **kwargs):
super(MyPaintElement, self).__init__(**kwargs)
self.active = False
def on_touch_down(self, touch):
# Check if touch event is inside widget
if self.collide_point(touch.x, touch.y):
self._draw_rectange()
def on_touch_move(self, touch):
# Check if touch event is inside widget
if self.collide_point(touch.x, touch.y):
# If so, draw rectangle
self._draw_rectange()
def _draw_rectange(self):
self.canvas.clear()
with self.canvas:
# lets draw a semi-transparent red square
Color(1, 1, 1, 1, mode='rgba')
Rectangle(pos=self.pos, size=(self.width*0.9, self.height*0.9))
self.active = True
def clear(self, color):
self.canvas.clear()
with self.canvas:
if color == "black":
# lets draw a semi-transparent red square
Color(0, 0.65, 0.65, 1, mode='rgba')
else:
Color(0, 0.2, 0.2, 1, mode='rgba')
Rectangle(pos=self.pos, size=(self.width*0.9, self.height*0.9))
self.active = False
def mark(self):
self._draw_rectange()
第二个类是我在“add_widget”部分中调用这个类的实例,它是继承GridLayout的类
class MyPaintWidget(GridLayout):
CODING_SIZE = 4
def __init__(self, size, **kwargs):
super(MyPaintWidget, self).__init__(**kwargs)
self.cols = size
for index in range(self.cols * self.cols):
self.add_widget(MyPaintElement())
def clear(self):
index = 0
#with self.canvas.before:
# Color(0, .1, .3, 1) # green; colors range from 0-1 instead of 0-255
# self.rect = Rectangle(size=self.size,
# pos=self.pos)
for child in self.children:
if index % 2:
child.clear("dark-turquoise")
else:
child.clear("black")
index += 1
def get_pattern(self):
# Initial pattern is an empty list of integers
pattern = []
# Integer representation or first row of pattern (bottom)
val = 0
# Counter to obtain integer value from binary row
count = 1
# For each MyPaintElement instance, verify if active and
# add integer value to val depending on its position (count)
for child in self.children:
if child.active:
val += count
count *= 2
# If row over, append to pattern array and
# start encoding new one
if count == pow(2, MyPaintWidget.CODING_SIZE):
pattern.append(val)
val = 0
count = 1
return pattern
def draw_pattern(self, pattern):
""" Draw given pattern in painter"""
for index in range(len(pattern)):
# Get children in groups of four (As codification was made by groups of four)
child_offset = index*MyPaintWidget.CODING_SIZE
child_set = self.children[child_offset:child_offset+MyPaintWidget.CODING_SIZE]
# Convert current number of pattern into binary
format_str = "{0:0"+str(MyPaintWidget.CODING_SIZE)+"b}"
bin_pattern_element = format_str.format(pattern[index])
# Traverse binary, mark or clear corresponding child
for j in range(len(bin_pattern_element)):
if bin_pattern_element[MyPaintWidget.CODING_SIZE-1-j] == "1":
child_set[j].mark()
else:
if j % 2:
child_set[j].clear("dark-turquoise")
else:
child_set[j].clear("black")
所以最后,它给我的错误是这个
File ".\main.py", line 513, in <module>
MyPaintApp().run()
File "C:\PATH\site-packages\kivy\app.py", line 829, in run
root = self.build()
File ".\main.py", line 509, in build
intentions_ui = IntentionsInterface()
File ".\main.py", line 244, in __init__
self.add_widgets_layouts()
File ".\main.py", line 364, in add_widgets_layouts
self.desired_state_panel = GridLayout(cols=1, padding_x = 10, size_hint_y = 1,size_hint_x=0.5)
File "C:\PATH\site-packages\kivy\uix\gridlayout.py", line 256, in __init__
super(GridLayout, self).__init__(**kwargs)
File "C:\PATH\site-packages\kivy\uix\layout.py", line 76, in __init__
super(Layout, self).__init__(**kwargs)
File "C:\PATH\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 exactly one argument (the instance to initialize)
所以,我想知道为什么会这样,我真的很感激任何建议或建议!
解决方案
我一直在寻找相同的解决方案,但我尝试以不同的方式“调用”该应用程序,并且它有效。尝试这个:
class MyApp(App):
def build(self):
return Label(text='Hello world')
#this is what i call "calling":
if __name__ == '__main__':
MyApp().run()
推荐阅读
- python - 抓取类元素
- azure - Azure Web 应用程序 - Godaddy 将 www 重定向到非 www
- flutter - 颤振中的并排组件
- azure - 配置 NGINX 以使用 SAML 签名证书
- javascript - 如何在悬停时连续旋转图标?
- python - 如何在 linux 中创建如下所示的内容(音量变化指示器)?
- apache-spark - 在 Spark 中,如何对大型数据帧/RDD 进行重新分区,以尽可能减少加入时的洗牌?
- python - 在 Imgaug 中使用图像增强时出现 Dtype 错误
- javascript - 如何使用 mongo 比较值和组?
- node.js - & 在查询字符串参数中未检测到