Python Kivy中的输入法

Python Kivy中的输入法,python,kivy,Python,Kivy,我试图在Kivy中获得连续的触摸运动来移动一个矩形,我为此编写了一个代码 from kivy.app import App from kivy.uix.button import Label from kivy.uix.widget import Widget from kivy.graphics import Rectangle from kivy.core.window import Window class Game(Widget): def __init__(self,**kw

我试图在Kivy中获得连续的触摸运动来移动一个矩形,我为此编写了一个代码

from kivy.app import App
from kivy.uix.button import Label
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle
from kivy.core.window import Window

class Game(Widget):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        with self.canvas:
            Color=(0,1,0,1)
            self.player=Rectangle(pos=(50,0),size=(20,50))



#touch and motion of our player
    def on_motion(self,etype,motionevent,**kwargs):
        print('hello')
        touch_condition=False
        x_touch=motionevent.spos[0]
        xa=self.player.pos[0]
        ya=self.player.pos[1]
        if etype=='begin' or etype=='update':
            touch_condition=True
        if etype=='end':
            touch_condition=False


        if touch_condition and x_touch>0.5:
            xa+=10
        if touch_condition and x_touch<0.5:
            xa-=10      

        self.player.pos=(xa,ya)

    Window.bind(on_motion=on_motion)



class Try(App):
    def build(self):
        return Game()

if __name__=="__main__":
    Try().run()
从kivy.app导入应用
从kivy.uix.button导入标签
从kivy.uix.widget导入widget
从kivy.graphics导入矩形
从kivy.core.window导入窗口
类游戏(小部件):
定义初始(自我,**kwargs):
超级()
使用self.canvas:
颜色=(0,1,0,1)
self.player=矩形(位置=(50,0),大小=(20,50))
#我们球员的触球和动作
def on_motion(self、etype、motionevent、**kwargs):
打印('你好')
触摸条件=错误
x_touch=motionevent.spos[0]
xa=self.player.pos[0]
ya=self.player.pos[1]
如果etype=='begin'或etype=='update':
接触条件=真
如果etype=='end':
触摸条件=错误
如果触摸条件和x触摸>0.5:
xa+=10

如果touch_condition和x_touch您的类结构混乱,则在类级别绑定
。我很惊讶这是你得到的错误,但这似乎是问题所在


Window.bind(on_motion=self.on_motion)
添加到游戏小部件的
初始化中。

您的类结构混乱,在类级别绑定
on_motion
。我很惊讶这是你得到的错误,但这似乎是问题所在

Window.bind(on_motion=self.on_motion)
添加到游戏小部件的
初始化中