python - Bind a button already on_press -


i want change function launched when button pressed.

python file:

class updatescreen(screen):  swimbot = {} def swimbot_connected(self):     wallouh = list(adbcommands.devices())     if not wallouh:         self.ids['text_label'].text = 'plug swimbot , try again'     else:         devices in adbcommands.devices():             output = devices.serial_number             if re.match("^(.*)swimbot", output):                 self.ids['mylabel'].text = 'etape 2: need 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 swimbot , try again' 

kv file :

<updatescreen>: boxlayout:     id: update_screen_layout     orientation: 'vertical'     label:         id: mylabel         text: "etape 1: connect 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() 

but think it's not right way :

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

with go directly check_need_update(), doesn't wait press button.

when put () after function in python execute function. when type

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

you pass result of self.check_needed_update on_press. need:

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

this different in kv language. there need put () when binding function. can put arguments there evaluated when callback called (and not when definition read).

but python code not want. bind additional function button, not overwrite other function. can unbind callbacks it's bit complicated (see the documentation here).

instead change called function in way behaves differently:

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

and instead of unbinding , binding new function button can modify updatescreen.state.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -