2017-10-20 163 views
0

因此,我创建了一些屏幕并为它们添加了一些属性(布局,按钮,名称等),但所有内容都在kivy文件中。现在,我想在python主文件中创建一个函数,这会在短时间后从开始屏幕到下一个屏幕。如果没有我必须将所有文件从kivy文件移动到python文件,似乎没有什么工作。有任何想法吗?.py文件与.kv文件进行交互

的.py

from kivy.app import App 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.lang import Builder 
from kivy.uix.image import Image 
from kivy.uix.label import Label 
from kivy.uix.behaviors import ButtonBehavior 


class MainScreen(Screen): 
    pass 

class AnotherScreen(Screen): 
    pass 


class AndAnotherScreen(Screen): 
    pass 


class ScreenManagement(ScreenManager): 
    pass 

class BackgroundLabel(Label): 
    pass 

class CompanyImage(Image): 
    pass 

class LoadingScreen(Screen): 
    def __init__(self, **kwargs): 
     super(LoadingScreen, self).__init__(**kwargs) 
    def ChangeScreen(self): 
     ScreenManager().current = MainScreen().name 



presentation = Builder.load_file("tsalapp.kv") 


class MainApp(App): 
    def build(self): 
     return presentation 


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

.kv

#: import FadeTransition kivy.uix.screenmanager.FadeTransition 
#:import Clock kivy.clock.Clock 
#: import Color kivy.graphics 
#: import Rectangle kivy.graphics 

ScreenManagement: 
    transition: FadeTransition() 
    LoadingScreen: 
    MainScreen: 
    AnotherScreen: 
    AndAnotherScreen: 

<BackgroundLabel>: 
    background_color: 30,144,255,1 
    canvas.before: 
     Color: 
      rgba: self.background_color 
     Rectangle: 
      pos: self.pos 
      size: self.size 

<LoadingScreen>: 
    name: "main" 
    id: "yolo" 
    on_enter: Clock.schedule_once(none, 3) 
    #app.root.current: "menu" 
    Image: 
     source: 'Untitled.png' 
     height: root.height 
     width: root.width 
     allow_stretch: True 
     keep_ratio: False 
    AnchorLayout: 
     Label: 
      text: "Tsala Inc." 
      anchor_x: 'center' 
      anchor_y: 'center' 
      font_size: root.height/15 





<MainScreen>: 
    name: "menu" 
    id: "swag" 
    BoxLayout: 
     orientation: "vertical" 
     Button: 
      text: "Next Page" 
      background_color: 1,1,4,1 
      size_hint: (0.5, 0.5) 
      on_release: 
       app.root.current= "Another Screen" 
     Button: 
      text: "Previous Page" 
      background_color: 1,4,1,1 
      size_hint: (0.5, 0.5) 
      on_release: 
       app.root.current= "And Another Screen" 

<AnotherScreen>: 
    name: "Another Screen" 
    GridLayout: 
     cols: 2 
     Button: 
      text: "Previous Page" 
      background_color: 0,0,1,1 
      size_hint: (0.25,1) 
      on_release: 
       app.root.current = "menu" 
     Button: 
      text: "Exit" 
      background_color: 1,0,0,1 
      size_hint: (0.25, 1) 
      on_release: 
       exit() 

<AndAnotherScreen>: 
    name: "And Another Screen" 
    BoxLayout: 
     orientation: "vertical" 
     Button: 
      text: "Next Page" 
      background_color: 1,1,4,1 
      size_hint: (0.5, 0.5) 
      on_release: 
       app.root.current= "menu" 
     Button: 
      text: "Nextest Page" 
      background_color: 1,4,1,1 
      size_hint: (0.5, 0.5) 
      on_release: 
       app.root.current= "Another Screen" 

回答

1

首先,我建议创建的ScreenManager或地方的ScreenManager在MainApp作为类属性的全局变量,并创建一个列表或字典屏幕:

class MyApp(App): 

    sm = ScreenManager() # screenmanager 
    screens = {} # dictionary for the screens, I prefer dictionary because string indexes are more convenient 

然后在一个构建方法中,你可以创建你所有的屏幕d和返回屏幕管理为根小部件:

class MyApp(App): 

    sm = ScreenManager() # screenmanager 
    screens = {} # dictionary for the screens, I prefer dictionary because string indexes are more convenient 

    def build(self): 
     self.__create_screens() 
     MyApp.sm.add_widget(MyApp.screens['main_screen']) 
     return MyApp.sm 

    def __create_screens(self): 
     MyApp.screens['main_screen'] = MainScreen(name='mainscreen') 
     MyApp.screens['another_screen'] = AnotherScreen(name='another_screen') 
     MyApp.screens['and_another_screen'] = AndAnotherScreen(name='and_another_screen') 

所以现在你可以轻松切换应用程序的屏幕,无论你想使用switch_to方法:

MyApp.sm.switch_to(MyApp.screens['another_screen'], direction='left', duration=1) 
+0

我尝试过,但它仍然似乎没有工作,我仍然不得不将所有从kv文件移动到python文件,这是我想避免的代码变得很奇怪。谢谢你的时间,虽然 – tsalamagewski

+0

@ tsalamagewski尝试返回建立的screenmanager(见更新)。 – saband