简体   繁体   English

如何使用 kivy 中的屏幕管理器在另一个 python 文件中运行 python 文件

[英]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.我有两个名为 a.py 和 b.py 的 python 文件,a.py 只是普通的 kivy 代码,而另一个 b.py 也是带有屏幕管理器的 kivy 代码。 The problem is this, i want to call a.py inside the b.py containing kivy screenmanger.问题是这样的,我想在包含 kivy 屏幕管理器的 b.py 中调用 a.py。 But it doesn't not run properly, it only runs the a.py file only.但它不能正常运行,它只运行 a.py 文件。 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:在您的b.py中,声明:

a.SplashApp().run()

starts the SplashApp .启动SplashApp But the run() call of an App does not return until the App stops.但是Apprun()调用在App停止之前不会返回。 So nothing after that statement is executed.所以在执行该语句之后什么都没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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