問題描述
我正在嘗試制作一個小型俄羅斯方塊游戲,用于使用 kivy 學習 python.我正在嘗試創建一個大小為 20,20 的自定義小部件.當我將它添加到浮動布局并運行以下代碼時,我收到以下錯誤:
I am trying to make a small tetris game for learning python with kivy. I am trying to create a custom widget with size 20,20. When I add it to the float layout and run the below code I receive the following error:
錯誤:
File "D:OS FilesworkspaceTetrisholder.py", line 10, in __init__ self.add_widget(c)
File "C:Kivy180kivykivyuixfloatlayout.py", line 115, in add_widget pos_hint=self._trigger_layout)
TypeError: descriptor 'bind' of 'kivy._event.EventDispatcher' object needs an argument
代碼:holder.py 文件:
Code: holder.py File:
from items import Cell
class Holder(FloatLayout):
def __init__(self, **kwargs):
super(Holder,self).__init__(**kwargs)
self.size=(300,300)
c=Cell
#c.pos= (20,20)
self.add_widget(c)
#self.add_widget(c)
items.py 文件:
items.py File:
from kivy.uix.widget import Widget
from kivy.graphics import *
class Cell(Widget):
def __init__(self, **kwargs):
super(Cell,self).__init__(**kwargs)
with self.canvas:
Color(1, 0, 0)
Rectangle(pos=(0, 0), size=(50, 50))
self.height=50
self.width=50
main.py 文件:
main.py File:
from kivy.app import App
from holder import Holder
class start(App):
def build(self):
return Holder()
if __name__ == '__main__':
start().run()
您能否解釋一下我哪里出錯了,我被困在起點本身.關于錯誤,我也沒有寫任何事件,它只是一個小部件類.能否請您解釋一下我在理解 kivy 方面出了什么問題.
Could you please explain where I went wrong, I am stuck at the starting point itself. Regarding the error, I haven't written any events also, and it is just a widget class. Could you please explain where I went wrong in understanding kivy.
推薦答案
c=Cell
我打賭你希望 c
成為 Cell
類的一個實例.如果你想這樣做,你需要這樣做:
I bet you want c
to be an instance of the Cell
class. If you want to do that, you need to do:
c=Cell()
這篇關于kivy自定義小部件綁定錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!