我一直在尝试解决此错误,当我尝试向继承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)
所以,我想知道为什么会这样,我非常感谢任何建议或推荐!
答案 0 :(得分:0)
我认为Grid布局的列未初始化。
super(MyPaintWidget, self).__init__(**kwargs)
需要修改为
super(MyPaintWidget, self).__init__(size)
答案 1 :(得分:0)
我一直在寻找相同的解决方案,但是我尝试以不同的方式“调用”该应用程序,并且该方法有效。试试这个:
class MyApp(App):
def build(self):
return Label(text='Hello world')
#this is what i call "calling":
if __name__ == '__main__':
MyApp().run()