2016-07-02 7 views
0

Kivy言語を使用してレイアウトされたボタンを関数にバインドする方法を理解しようとしています。私はanswersをPython言語でボタンをレイアウトするときにたくさん見てきました。しかし、いったんすべてが整ったら、Buttonから継承するカスタムクラスを使って参照していますか?Kivy言語でボタンを追加した後にボタンをバインドする

次のコードでは、エラーTypeError: show() takes 1 positional argument but 2 were givenがスローされ、プログラムがクラッシュします。

class TimerButton(ButtonBehavior, Image): 
    timer_container = ObjectProperty(None) 
    client_scoreboard = ObjectProperty(None) 

    def __init__(self, **kwargs): 
     super(TimerButton, self).__init__(**kwargs) 
     self.bind(on_press=self.show) 
     self.bind(on_release=self.changeImage) 

    def show(self): 
     print('hi there') 
     self.source = 'assets/mainViewTimerButtonPressed.png' 
     import kivy.animation as Animate 
     anim = Animate.Animation(
      x=self.client_scoreboard.right - self.timer_container.width, 
      y=self.timer_container.y, 
      duration=0.25) 
     anim.start(self.timer_container) 
     self.unbind(on_press=self.show) 
     self.bind(on_press=self.hide) 

    def changeImage(self): 
     self.source = 'assets/mainViewTimerButton.png' 

    def hide(self): 
     import kivy.animation as Animate 
     anim = Animate.Animation(
      x=self.client_scoreboard.width, 
      y=self.timer_container.y, 
      duration=0.25) 
     anim.start(self.timer_container) 
     self.unbind(on_press=self.hide) 
     self.bind(on_press=self.show) 

答えて

0

答えは、関数定義では、この場合TimerButtonには、クラス名を含めることです。これは、関数がTimerButtonクラスのスコープ内で定義されているため、私が完全に理解していない概念です。しかし、ねえ、それは動作します。

class TimerButton(ButtonBehavior, Image): 
    timer_container = ObjectProperty(None) 
    client_scoreboard = ObjectProperty(None) 

    def __init__(self, **kwargs): 
     super(TimerButton, self).__init__(**kwargs) 
     self.bind(on_press=self.show) 
     self.bind(on_release=self.changeImage) 

    def show(self): 
     print('hi there') 
     self.source = 'assets/mainViewTimerButtonPressed.png' 
     import kivy.animation as Animate 
     anim = Animate.Animation(
      x=self.client_scoreboard.right - self.timer_container.width, 
      y=self.timer_container.y, 
      duration=0.25) 
     anim.start(self.timer_container) 
     self.bind(on_press=self.hide) 

    def changeImage(self): 
     self.source = 'assets/mainViewTimerButton.png' 

    def hide(self): 
     import kivy.animation as Animate 
     anim = Animate.Animation(
      x=self.client_scoreboard.width, 
      y=self.timer_container.y, 
      duration=0.25) 
     anim.start(self.timer_container) 
     self.bind(on_press=self.show) 
0

あなたは.bind()に設定された機能は、あなたの関数がのために準備されていない引数を渡して呼び出すkivyコード。最後にkivyを使用してからしばらくしているので、私は確信が持てませんが、イベントの詳細が関数に渡されると思います。イベントハンドラの

このよう

、あなたの定義は次のようになります。あなたは好奇心旺盛であれば

def show(self, event): 
    ... 

def hide(self, event): 
    ... 

は、あなたがそれらの関数内print(event)が、中に送信されていますかを確認することができ

関連する問題