[Kivy] Chagne a button's function after its first pressed - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Kivy] Chagne a button's function after its first pressed (/thread-16110.html) |
Chagne a button's function after its first pressed - TheStraying11 - Feb-14-2019 I'm making a hidden object game, and I decided to use Kivy because I wanted to try it out. One of the triggers for finding one of the hidden objects is clicking on the text "Not here" after the player has clicked this button, I want the on_press function to be changed so that the button no longer does anything. when I run my current code I get the error 'AssertionError: None is not callable' in sdl2 here is my current code, the place I assign the dummy function 'nothing()' is in the clk() function from kivy.app import App from kivy.uix.button import Button from kivy.uix.boxlayout import BoxLayout class myLayout(BoxLayout): def __init__(self, **kwargs): super(myLayout, self).__init__(**kwargs) global btn btn = Button(text = "Not here") btn.bind(on_press=self.clk) self.add_widget(btn) def nothing(self): do = 'nothing' def clk(self, obj): print("you found one") btn.bind(on_press=self.nothing()) class GameApp(App): def build(self): mL = myLayout() self.title = "We don't have any chemical weapons" self.icon = "Hussein.ico" return mL if __name__ == "__main__": GameApp().run()here's the full error
RE: Chagne a button's function after its first pressed - Larz60+ - Feb-14-2019 Initially, in __init__, set: self.func_to_run = self.clk change button bind to: btn.bind(on_press=func_to_run) then when you want to change what btn does: self.func_to_run = self.new_func
RE: Chagne a button's function after its first pressed - Yoriz - Feb-17-2019 btn.bind(on_press=self.nothing())should not have open/close brackets after self.nothing, by doing this the bind is to the result of self.nothing which is None change it to btn.bind(on_press=self.nothing) |