Arkyris
Arkyris

Reputation: 85

Change screens with python logic (Kivy Screen manager)

I can't find the syntax for setting on_press in python code to change the screen anywhere. I keep getting errors for anything like Button(text = 'hi', on_press = self.current = 'start_menu. Here's the code and it works as is.

class LoadMenu(Screen):
    def __init__(self, **kwargs):
        super(LoadMenu, self).__init__(**kwargs)
        Clock.schedule_once(self.update)

    def update(self, dt):
        L = [x for x in range(len(os.listdir('saves')))]
        for x in L:
            x = self.add_widget(Button(text = os.listdir('saves')[x]))

I haven't positioned the buttons so they just are on top of each other but I can fix that later. What I need to happen is for each button to change to the play screen on press so that will be the same for each button but I also need each one to load the Shelve file they a referencing.(I know I'll need another function for that) Can I have an on_press trigger two events at once, and again how do I set it in the python code?

Upvotes: 0

Views: 1466

Answers (1)

zeeMonkeez
zeeMonkeez

Reputation: 5157

Consider the following programme:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.properties import StringProperty

dirlist = ['aaa', 'bbb', 'ccc', 'ddd']

class MyButton(Button):
    prop = StringProperty('')
    def on_press(self):
        print "Class-defined on_press handler (I'm {})".format(self.text)

def other_on_press_handler(sender):
    print "other_on_press_handler, from {}".format(sender.text)

def some_func(text):
    print "yeah: " + text

class LoadMenu(Screen):
    def __init__(self, **kwargs):
        super(LoadMenu, self).__init__(**kwargs)
        Clock.schedule_once(self.update)

    def on_press_handler(self, sender):
        print "on_press_handler, from {}".format(sender.text)
        self.parent.current = 'sc2'

    def yet_another_on_press_handler(self, sender):
        print "yet_another_on_press_handler, from {}".format(sender.text)
        self.parent.current = 'sc2'

    def update(self, dt):
        for x in range(len(dirlist)):
            my_b = Button(text = dirlist[x], on_press=self.on_press_handler)
            self.parent.ids.button_container.add_widget(my_b)
            if x > 1:
                my_b.bind(on_press=other_on_press_handler)
            if x == 3:
                my_b.bind(on_press=lambda sender: some_func("Whoa, lambda was here ({})".format(sender.text)))
        for x in range(len(dirlist)):
            my_b = MyButton(text = 'my '+ dirlist[x], prop="{} {}".format(dirlist[x], x))
            self.parent.ids.button_container.add_widget(my_b)
            my_b.bind(on_press=self.yet_another_on_press_handler)

root = Builder.load_string("""
ScreenManager:
    LoadMenu:
        name: 'sc1'
        GridLayout:
            cols: 4
            id: button_container
    Screen:
        name: 'sc2'
        BoxLayout:
            Button:
                text: "Go back"
                on_press: root.current = 'sc1'
""")

class MyApp(App):
    def build(self):
        return root

if __name__ == '__main__':
    a = MyApp()
    a.run()

Let's start by looking at the update method in LoadMenu: In the first loop, a bunch of buttons is generated, and each receives an on_press callback at creation. The last two buttons in the loop get bound to another callback, and the last example shows how to use a lambda expression to generate a callback. In the second for loop, we instantiate object of class MyButton, a child of Button. Note that we also define an on_press handler in the class definition; this gets called in addition to other functions we may have bound. But really, this is actually all pretty nicely explained in the kivy Events and Properties docs.

Upvotes: 1

Related Questions