Python Forum
[Kivy] Chagne a button's function after its first pressed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Kivy] Chagne a button's function after its first pressed
#1
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
Error:
[INFO ] [Base ] Leaving application in progress... Traceback (most recent call last): File "C:/Users/youre/Desktop/project/main.py", line 28, in <module> GameApp().run() File "C:\Program Files\Python37\lib\site-packages\kivy\app.py", line 826, in run runTouchApp() File "C:\Program Files\Python37\lib\site-packages\kivy\base.py", line 502, in runTouchApp EventLoop.window.mainloop() File "C:\Program Files\Python37\lib\site-packages\kivy\core\window\window_sdl2.py", line 727, in mainloop self._mainloop() File "C:\Program Files\Python37\lib\site-packages\kivy\core\window\window_sdl2.py", line 460, in _mainloop EventLoop.idle() File "C:\Program Files\Python37\lib\site-packages\kivy\base.py", line 340, in idle self.dispatch_input() File "C:\Program Files\Python37\lib\site-packages\kivy\base.py", line 325, in dispatch_input post_dispatch_input(*pop(0)) File "C:\Program Files\Python37\lib\site-packages\kivy\base.py", line 231, in post_dispatch_input listener.dispatch('on_motion', etype, me) File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "C:\Program Files\Python37\lib\site-packages\kivy\core\window\__init__.py", line 1360, in on_motion self.dispatch('on_touch_down', me) File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "C:\Program Files\Python37\lib\site-packages\kivy\core\window\__init__.py", line 1376, in on_touch_down if w.dispatch('on_touch_down', touch): File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "C:\Program Files\Python37\lib\site-packages\kivy\uix\widget.py", line 460, in on_touch_down if child.dispatch('on_touch_down', touch): File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "C:\Program Files\Python37\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down self.dispatch('on_press') File "kivy\_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch File "kivy\_event.pyx", line 1214, in kivy._event.EventObservers.dispatch File "kivy\_event.pyx", line 1138, in kivy._event.EventObservers._dispatch File "C:/Users/youre/Desktop/project/main.py", line 19, in clk btn.bind(on_press=self.nothing()) File "kivy\_event.pyx", line 419, in kivy._event.EventDispatcher.bind AssertionError: None is not callable
Reply
#2
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
Reply
#3
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a function interrupt button tkinter AnotherSam 2 5,412 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,916 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  TKinter restarting the mainloop when button pressed zazas321 7 16,085 Jan-26-2021, 06:38 AM
Last Post: zazas321
  Class function does not create command button Heyjoe 2 2,223 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  Tkinter:Unable to bind and unbind function with a button shallanq 2 4,963 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  [Tkinter] How to bind an event when enter is pressed on a Entry control? Michael4 4 3,878 Aug-29-2019, 10:11 PM
Last Post: Michael4
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,947 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  GUI freezes while executing a callback funtion when a button is pressed abi17124 5 7,388 Jul-10-2019, 12:48 AM
Last Post: FullOfHelp
  [Tkinter] Button won't execute function TheLegendOfPanda 2 3,028 Jul-05-2019, 07:41 PM
Last Post: TheLegendOfPanda
  [Tkinter] How make a button perform a function after the user inputs numbers Zephyrforce 1 2,413 May-22-2019, 05:43 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020