简体   繁体   中英

How do I run a python file inside another python file with Screen manager in kivy

I have two python file called a.py and b.py, the a.py is just plain kivy code while the other b.py is also kivy code with Screen manager. The problem is this, i want to call a.py inside the b.py containing kivy screenmanger. But it doesn't not run properly, it only runs the a.py file only. I am doing this to segment my code because it would become larger.

# a.py file


import kivy
from kivy.app import App
from kivy.uix.floatlayout import Floatlayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.label import Label 


class LandingScreen(FloatLayout):
    def __init__(self, **kwargs):
        super(LandingScreen, self).__init__(**kwargs)

         
        self.btn1=Button(text='button1 ', size_hint=(0.5, 0.5), 
        on_press=self.click_b1))
        self.btn2=Button(text='button2', size_hint=(0.5, 0.5), 
        on_press=self.click_b2))


            
        self.add_widget(self.btn1)
        self.add_widget(self.btn2)

        def click_b1(self, instance):
             
             pass
        def click_b2(self, instance):
             pass
       
class SplashApp(App):
    def build(self):
        return LandingScreen()

if __name__ == '__main__':

# b.py file containing screemanager

import a

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition


class ScreenManagement(ScreenManager):
    def __init__(self, **kwargs):
        super(ScreenManagement, self).__init__(**kwargs)


class RegisterWindow(Screen):
    def __init__(self, **kwargs):
        super(RegisterWindow, self).__init__(**kwargs)
        
    a.SplashApp().run()


class LoginWindow(Screen):
        def __init__(self, **kwargs):
            super(LoginWindow, self).__init__(**kwargs)
            self.btn2 = Button(text='Go')
            self.add_widget(self.btn2)
            self.btn2.bind(on_press = self.screen_transition)

        def screen_transition(self, *args):
            self.manager.current = 'register'


class Application(App):
    def build(self):
        sm = ScreenManagement(transition=FadeTransition())
        sm.add_widget(LoginWindow(name='login'))
        sm.add_widget(RegisterWindow(name='register'))
        return sm


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

In your b.py , the statement:

a.SplashApp().run()

starts the SplashApp . But the run() call of an App does not return until the App stops. So nothing after that statement is executed.

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