簡體   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