1

I'm working on getting an option menu that is the same throughout all of my screens. The option menu is a Popup with a few options. When the popup is called, it doesn't save the inputs. When the Switch button is changed to inactive, it defaults back to active after the user closes and reopens the popup.

How do I save the user's inputs?

from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.switch import Switch

Builder.load_string('''

<OptionMenu@Button>:
    text: "Option Menu"
    size_hint_y: .3
    on_press:
        app.option_menu_function()

<BoxLayout@BoxLayout>:
    orientation: "vertical"

<HomeScreen>:
    BoxLayout:
        OptionMenu:
        Label:
            text: "Home Screen"
        Button:
            text: "Next Screen >"
            on_release:
                app.root.current = "screen1"
<Screen1>:
    BoxLayout:
        OptionMenu:
        Label:
            text: "Screen 2"
        Button:
            text: "Next Screen >"
            on_release:
                app.root.current = "homescreen"
<SM>:
    name: "sm"
    id: sm
    HomeScreen:
        name: "homescreen"
        id: hs
    Screen1:
        name: "screen1"
        id: s1
''')

class HomeScreen(Screen):
    pass
class Screen1(Screen):
    pass
class SM(ScreenManager):
    pass

class MyApp(App):

    def build(self):
        global sm
        sm = SM()
        return sm

    def option_menu_function(self):
        box = BoxLayout(orientation = "horizontal")
        switch = Switch(active = True)
        popup = Popup(content = box, auto_dismiss = True, size_hint = (None, None), size = (Window.width/1.5, Window.width/1.5))
        box.add_widget(switch)
        popup.open()

if __name__ == "__main__":
    MyApp().run()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Petar Luketina
  • 449
  • 6
  • 18

1 Answers1

1

The reason why it does not save the status of the widget is that you are creating a new widget at every moment, so the solution is to reuse. On the other hand avoid using global variables because they can cause problems, are cases where they must be used. Considering the above, the solution is as follows:

class MyApp(App):
    def build(self):
        box = BoxLayout(orientation = "horizontal")
        box.add_widget(Switch(active = True))
        # a single popup is created
        self.popup = Popup(content = box, auto_dismiss = True, size_hint = (None, None), size = (Window.width/1.5, Window.width/1.5))

        self.root = SM()
        return self.root

    def option_menu_function(self):
        self.popup.open()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • **eyllanesc** thank you, thank you!! I can't believe that worked. I tried dozens of ways to make this happen... I still don't understand how yours works flawlessly. They main difference between our codes is my use of `global sm` and your `return self.root`, right? Can you explain why yours is effective, and perhaps why globals aren't that effective. Thank you again for opening my eyes to my flaws. – Petar Luketina Oct 22 '18 at 06:31
  • 1
    @PetarLuketina Here it has nothing to do with global or not (but please avoid using them : https://stackoverflow.com/questions/19158339/why-are-global-variables-evil). – eyllanesc Oct 22 '18 at 06:36
  • Let's say that there is a factory of people, so every time you call that factory you get a baby, you feed it and it grows, let's say that it already knows how to speak, but if you call it back to the factory it will send you a baby that can not speak so you always get a baby, that's what happens with Popup you're calling and creating a baby Popup that is different from the one you called earlier, in my solution only a Popup is created, and I reuse it, it's similar to having the baby who already knows how to speak, I no longer call the factory I only call the baby I already have. – eyllanesc Oct 22 '18 at 06:36
  • ah okay. Thank you for the explanation! – Petar Luketina Oct 22 '18 at 13:20