繁体   English   中英

Button'对象没有属性python回调

[英]Button' object has no attribute python callback

我想为btn1提供回调函数,以更改player1.center_y并与我的程序进行一般交互(更改Gameclass中的值)。

但是我得到了错误

单击按钮时,“ 按钮”对象没有属性“玩家1 ”。 是因为实例类有错误还是什么?我对OOP有点陌生。

这是相关代码:

class Gameclass(Widget):
    def on_touch_move(self, touch):
        if touch.x < self.width / 3:
            self.player1.center_y = touch.y
        if touch.x > self.width - self.width / 3:
            self.player2.center_y = touch.y

    def callback(self):
            self.player1.center_y = 22
            self.player2.center_y = 250

btn1 = Button(text='click me')
btn1.bind(on_press=Gameclass.callback)

我的评论可能很难理解,因此这里有一个例子:

class Gameclass(Widget):
    def on_touch_move(self, touch):
        if touch.x < self.width / 3:
            self.player1.center_y = touch.y
        if touch.x > self.width - self.width / 3:
            self.player2.center_y = touch.y

    def callback(self, btn_instance):
            self.player1.center_y = 22
            self.player2.center_y = 250

btn1 = Button(text='click me')
gc = Gameclass()
btn1.bind(on_press=gc.callback)

请注意,您可能在代码中的其他位置创建了Gameclass的实例,并且应该使用该实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM