【问题标题】:Kivy Multiple Screen Transitions not taking placeKivy 多屏幕转换未发生
【发布时间】:2016-10-10 13:03:56
【问题描述】:

我正在尝试在 python 中构建一个多屏 kivy 应用程序。没有编译时错误。应用程序编译成功。我在 kivy 中使用 Screen Manager 来实现多个屏幕。单击按钮时,不会发生任何转换。请帮我执行转换。这是我的代码的实际 sn-ps。

ma​​in.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'

【问题讨论】:

  • 很高兴接受这个答案

标签: python kivy


【解决方案1】:

将屏幕管理器声明为全局变量

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

并在build 方法中返回screen_manager 实例。

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'

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多