2017-10-20 6 views
0

J'ai donc créé des écrans et ajouté des propriétés (layouts, boutons, noms, etc.) mais tout est dans le fichier kivy. Maintenant, je veux faire une fonction dans le fichier principal python qui va me prendre de l'écran de démarrage au suivant après un court moment. Bien que rien ne semble fonctionner sans que je doive tout déplacer du fichier kivy au fichier python. Des idées?Le fichier .py interagit avec le fichier .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" 

Répondre

1

Tout d'abord, je recommande de créer une variable globale pour ScreenManager ou un lieu ScreenManager dans la MainApp comme propriété de classe et de créer une liste ou un dictionnaire pour les écrans :

class MyApp(App): 

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

Ensuite, dans une méthode de construction, vous pouvez créer tous les écrans dont vous avez besoin. d et retourner le ScreenManager comme widget racine:

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') 

Alors maintenant, vous pouvez passer vos écrans d'applications facilement partout où vous voulez en utilisant la méthode switch_to:

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

Je l'ai essayé, mais il ne semble pas encore travailler, je Je devrais tout déplacer du fichier kv dans le fichier python, ce que je veux éviter car le code devient assez bizarre. Merci pour votre temps si – tsalamagewski

+0

@tsalamagewski essayer de retourner screenmanager sur la construction (voir mise à jour). – saband