Python Kivy:TypeError:object.\uuuu init\uuuuu()只接受一个参数(要初始化的实例)
我一直在尝试修复这个错误,当我尝试向继承Gridlayout的类添加一个小部件时,我不知道继承小部件的类是问题所在还是Gridlayout问题所在,我一直在尝试做很多没有结果的事情,所以我想问一下这是否有解决方案 第一个类是继承小部件的类Python Kivy:TypeError:object.\uuuu init\uuuuu()只接受一个参数(要初始化的实例),python,python-3.x,kivy,Python,Python 3.x,Kivy,我一直在尝试修复这个错误,当我尝试向继承Gridlayout的类添加一个小部件时,我不知道继承小部件的类是问题所在还是Gridlayout问题所在,我一直在尝试做很多没有结果的事情,所以我想问一下这是否有解决方案 第一个类是继承小部件的类 class MyPaintElement(Widget): def __init__(self, **kwargs): super(MyPaintElement, self).__init__(**kwargs) self.active = F
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)
文件“\main.py”,第513行,在
MyPaintApp().run()
文件“C:\PATH\site packages\kivy\app.py”,第829行,正在运行
root=self.build()
文件“\main.py”,第509行,内部版本
意向\u ui=IntentionsInterface()
文件“\main.py”,第244行,在__
self.add_widgets_layouts()
文件“\main.py”,第364行,在add\u widgets\u布局中
self.desired\u state\u panel=GridLayout(cols=1,padding\u x=10,size\u hint\u y=1,size\u hint\u x=0.5)
文件“C:\PATH\site packages\kivy\uix\gridlayout.py”,第256行,在\uuu init中__
超级(网格布局,自我)。\uuuuu初始化(**kwargs)
文件“C:\PATH\site packages\kivy\uix\layout.py”,第76行,在\uuu init中__
超级(布局,自我)。\uuuuu初始化(**kwargs)
文件“C:\PATH\site packages\kivy\uix\widget.py”,第350行,在\uuu init中__
超级(小部件,自我)。\uuuuu初始化(**kwargs)
文件“kivy\\u event.pyx”,第243行,在kivy.\u event.EventDispatcher.\uu init中__
TypeError:object.\uuuu init\uuuuuuuu()只接受一个参数(要初始化的实例)
所以,我想知道为什么会发生这种情况,我真的很感激任何建议或建议 我认为网格布局的列没有初始化
super(MyPaintWidget, self).__init__(**kwargs)
需要修改为
super(MyPaintWidget, self).__init__(size)
我一直在寻找相同的解决方案,但我尝试以不同的方式“调用”该应用程序,结果成功了。试试这个:
class MyApp(App):
def build(self):
return Label(text='Hello world')
#this is what i call "calling":
if __name__ == '__main__':
MyApp().run()
你好我尝试了这个,但它抛出了一个错误,
super(MyPaintWidget,self)。\uuuu init\uuuu(size)TypeError:\uuuuu init\uuuuu()接受1个位置参数,但给出了2个