Skip to content

Instantly share code, notes, and snippets.

@ilovetogetspamed
Created September 26, 2016 16:09
Show Gist options
  • Save ilovetogetspamed/f4ee4e918346e9d2c54d517a171b5df8 to your computer and use it in GitHub Desktop.
Save ilovetogetspamed/f4ee4e918346e9d2c54d517a171b5df8 to your computer and use it in GitHub Desktop.
Can't get on_current_user to fire. Trying to figure out how to use EventDispatcher class.
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.uix.button import Button
from kivy.event import EventDispatcher
from kivy.properties import DictProperty
class UserManger(EventDispatcher):
current_user = DictProperty({'rfid_tag': '', 'nickname': '', 'logged_in': False})
# This never fires!
def on_current_user(self, instance, value):
print("Typical property change from", obj, "to", value)
class DemoBox(BoxLayout):
# add a current user for the app
current_user = UserManger()
def __init__(self,**kw):
"""Constructor: make a new panel with a button"""
super(DemoBox,self).__init__(**kw)
button = Button(text='Click Me!',size_hint=(1,1))
self.add_widget(button)
# Set the callback function
button.on_press = self.my_callback
def my_callback(self):
"""Function to call when button is pressed."""
print 'Hello World!'
self.current_user.rfid_tag = 'ABCDEFG'
self.current_user.nick_name = 'Santa Claus'
self.current_user.logged_in = True
print self.current_user.rfid_tag
self.current_user.rfid_tag = 'QWERTY1'
print self.current_user.rfid_tag
print
class DemoApp(App):
def build(self):
return DemoBox()
if __name__ == "__main__":
DemoApp().run()
@ilovetogetspamed
Copy link
Author

from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.uix.button import Button

from kivy.event import EventDispatcher
from kivy.properties import DictProperty

class UserManger(EventDispatcher):
    current_user = DictProperty({'rfid_tag': '', 'nickname': '', 'logged_in': False})

    # This fires now, after much refactoring... Thanks rafalo1333!
    def on_current_user(self, instance, value):
        print "Typical property change from {} to {})".format(instance, value)


class DemoBox(BoxLayout):

    # add a current user for the app
    user_manager = UserManger()

    def __init__(self,**kw):
        """Constructor: make a new panel with a button"""
        super(DemoBox,self).__init__(**kw)

        button = Button(text='Click Me!',size_hint=(1,1))
        self.add_widget(button)

        # Set the callback function
        button.on_press = self.my_callback

    def my_callback(self):
        """Function to call when button is pressed."""
        print 'Hello World!'

        self.user_manager.current_user['rfid_tag'] = 'ABCDEFG'
        self.user_manager.current_user['nick_name'] = 'Santa Claus'
        self.user_manager.current_user['logged_in'] = True
        print self.user_manager.current_user['rfid_tag']

        self.user_manager.current_user['rfid_tag'] = 'QWERTY1'
        print self.user_manager.current_user['rfid_tag']
        print

class DemoApp(App):
    def build(self):
        return DemoBox()

if __name__ == "__main__":
    DemoApp().run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment