2016-10-10 27 views
-1

我想在python中构建一个多屏kivy应用程序。没有编译时错误。应用程序成功编译。我在kivy中使用屏幕管理器来实现多个屏幕。点击按钮时不会发生转换。请帮助我执行转换。这里是我的代码的实际片段。Kivy多屏转换没有发生

main.py文件

import kivy 

from kivy.app import App 
from kivy.app import ObjectProperty 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.screenmamager import ScreenManager, Screen 

class LoginScreen(Screen): 
    pass 

class SignUpScreen(Screen): 
    pass 

class MainScreen(BoxLayout): 
    pass 

class MyScreenManager(ScreenManager): 
    pass 

class AuthenticationApp(App): 
    def build(self): 
     return MyScreenManager() 

if __name__ == '__main__': 
    AuthenticationApp().run() 

Authentication.kv文件

<MyScreenManager> 
    MainScreen: 
    SecondScreen: 

<SecondScreen>: 
    name: 'Second' 
    BoxLayout: 
     orientation: 'vertical' 

     canvas: 
      Rectangle: 
       source: 'images/blue.png' 
       pos: self.pos 
       size: self.size 

     BoxLayout: 
      orientation: 'vertical' 
      size_hint: 1,0.25 

      Label: 
       text: 'Vigilantdsjkadhakjshdakjsd Dollop' 
       font_size: '15sp' 
       size_hint: 1, 0.20 


      BoxLayout: 
       orientation: 'horizontal' 
       size_hint: 1, 0.1 

       Button: 
        id: login_button 
        text: 'Login' 
        font_size: '15sp' 
        on_release: app.root.current = 'Main' 

       Button: 
        id: login_button 
        text: 'Sign Up' 
        font_size: '15sp' 

       Button: 
        id: login_button 
        text: 'Recover' 
        font_size: '15sp' 

       Button: 
        id: login_button 
        text: 'Reset' 
        font_size: '15sp' 

     BoxLayout: 
      orientation: 'vertical' 
      size_hint: 1,0.75 

      Button: 
       text: 'Page' 



<MainScreen>: 
    name: 'Main' 
    BoxLayout: 
     orientation: 'vertical' 

     canvas: 
      Rectangle: 
       source: 'images/blue.png' 
       pos: self.pos 
       size: self.size 

     BoxLayout: 
      orientation: 'vertical' 
      size_hint: 1,0.25 

      Label: 
       text: 'Vigilant Dollop' 
       font_size: '15sp' 
       size_hint: 1, 0.20 


      BoxLayout: 
       orientation: 'horizontal' 
       size_hint: 1, 0.1 

       Button: 
        id: login_button 
        text: 'Login' 
        font_size: '15sp' 

       Button: 
        id: login_button 
        text: 'Sign Up' 
        font_size: '15sp' 
        on_press: root.current = 'Second' 

       Button: 
        id: login_button 
        text: 'Recover' 
        font_size: '15sp' 

       Button: 
        id: login_button 
        text: 'Reset' 
        font_size: '15sp' 

     BoxLayout: 
      orientation: 'vertical' 
      size_hint: 1,0.75 

      Button: 
       text: 'Page' 
+0

这将是很好的接受的答案 – Juggernaut

回答

0

声明屏幕管理程序的全局变量

import kivy 

from kivy.app import App 
from kivy.app import ObjectProperty 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.screenmamager import ScreenManager, Screen 

screen_manager = ScreenManager() 

class LoginScreen(Screen): 
    pass 

,并返回screen_manager斯塔在build方法中。

class AuthenticationApp(App): 
    def build(self): 
     screen_manager.add_widget(LoginScreen(name='login')) 
     return sceen_manager 

然后在您的.kv文件中您想要在屏幕之间切换的位置。试试例如:

Button: 
    id: login_button 
    text: 'Login' 
    font_size: '15sp' 
    on_release: root.manager.current = 'login' 
+0

非常感谢的人。它工作得很好! –