简体   繁体   中英

How to call a function in Python from another screen class?

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanger import ScreenManager, Screen

Builder.load_string("""
<MenuScreen>:
    BoxLayout:
        FloatLayout:
            Image:
                id: homebackg
                pos: 0, 0
                source: "dark.png"
<SettingsScreen>:
    BoxLayout:
        FloatLayout:
            Image:
                id: settingsbackg
                pos: 0, 0
                source: "dark.png"
            Button:
                text: ""
                color: 1.0, 1.0, 1.0, 0.0
                on_press:
                    root.buttonclick()
""")

class MenuScreen(Screen):
    def darkmode(self):
        self.ids.homebackg.source = "dark.png"
    def litemode(self):
        self.ids.homebackg.source = "lite.png"

class SettingsScreen(Screen):
    def darkmode(self):
        self.ids.settingsbackg.source = "dark.png"
    def litemode(self):
        self.ids.settingsbackg.source = "lite.png"
    def buttonclick(self):
        file = open("stat.txt", "r")
        themestat = file.read()
        file.close()
        if themestat == "lite":
            SettingsScreen.darkmode(self)
            MenuScreen.darkmode(self)
            file = open("stat.txt", "w")
            file.write("dark")
            file.close()
        elif themestat == "dark":
            SettingsScreen.litemode(self)
            MenuScreen.litemode(self)
            file = open("stat.txt", "w")
            file.write("lite")
            file.close()

sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))

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

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

Assume I have done the screen transitions properly not adding to make code less bulky, I have issue with changing background of menuscreen from the settingsscreen. It is for darkmode you see, thanks in advance. Self is not defined when executing MenuScreen.darkmode(self) from SettingsScreen.

You problem is that you are adding objects of screens to screen manager (and you will see only these objects) and then you are creating new objects and make changes there. So since you are using global variables the simplest solution would be:

sm = ScreenManager()
menuscreen = MenuScreen(name='menu')
settingsscreen = SettingsScreen(name='settings')
sm.add_widget(menuscreen)
sm.add_widget(settingsscreen)

And inside the classes you can just make changes to them like:

menuscreen.darkmode()

PS 'self' means object of the class, in Python self is used to have access to class variables and functions from functions defined in that class. And here you don't need to send 'self' to another class because you're sending object of SettingsScreen class to MenuScreen darkmode function and do nothing with it. If you want to send object there should be:

# you send object of SettingsScreen
menuscreen.darkmode(self)

# and receive it as obj for example
def darkmode (self, obj):
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM