2016-09-22 13 views
0

ボタンを押したときに起動する機能を変更したいと思います。ボタンを既にバインドしています

Pythonのファイル:

class UpdateScreen(Screen): 

swimbot = {} 
def swimbot_connected(self): 
    wallouh = list(AdbCommands.Devices()) 
    if not wallouh: 
     self.ids['text_label'].text = 'Plug your Swimbot and try again' 
    else: 
     for devices in AdbCommands.Devices(): 
      output = devices.serial_number 
      if re.match("^(.*)swimbot", output): 
       self.ids['mylabel'].text = 'Etape 2: Do you need an update ?' 
       self.ids['action_button'].text = 'Check' 
       self.ids['action_button'].bind(on_press = self.check_need_update()) 
      else: 
       self.ids['text_label'].text = 'Plug your Swimbot and try again' 

のKvファイル:

<UpdateScreen>: 
BoxLayout: 
    id: update_screen_layout 
    orientation: 'vertical' 
    Label: 
     id: mylabel 
     text: "Etape 1: Connect your Swimbot" 
     font_size: 26 
    Label: 
     id: text_label 
     text: "Truc" 
     font_size: 24 
    FloatLayout: 
     size: root.size 
     pos: root.pos 
     Button: 
      id: action_button 
      pos_hint: {'x': .05, 'y':.25} 
      size_hint: (.9, .4) 
      text: "Try" 
      font_size: 24 
      on_press: root.swimbot_connected() 

しかし、私はそれがこれをすることを行うには正しい方法ではないと思う。それに

self.ids['action_button'].bind(on_press = self.check_need_update()) 

私が直接行きますcheck_need_update()に、私はボタンを押すのを待つことはありません。

答えて

0

Pythonで関数の後に()を置くと、その関数が実行されます。あなたは

self.ids['action_button'].bind(on_press = self.check_need_update()) 

を入力するときにあなたが実際にon_pressにself.check_needed_updateの結果を渡します。したがって、あなたが必要です:

self.ids['action_button'].bind(on_press = self.check_need_update) 

これはkv言語では異なります。実際には、関数をバインドするときには()を置く必要があります。コールバックが呼び出されたときに評価される(そして定義が読み込まれるときではない)引数をそこに置くこともできます。

しかし、実際にはPythonコードはあなたが望むことをしません。そのボタンに追加機能をバインドしますが、他の機能を上書きしません。コールバックをアンバインドすることはできますが、少し複雑です(the documentation here参照)。

代わりに、私はそれが異なった動作をする方法で呼び出される関数に変更になります。

class UpdateScreen(Screen): 

    state = 0 
    swimbot = {} 
    def swimbot_connected(self): 
     if state == 0: 
      self._original_swimbot_connected() 
     if state == 1: 
      self.check_need_update 

そして代わりのアンバインドとあなただけのUpdateScreen.stateを変更することができ、ボタンに新しい機能を結合します。

関連する問題