2017-10-20 52 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