.py文件中的python- Kivy Screen Manager [英] python- Kivy Screen Manager within .py file

查看:53
本文介绍了.py文件中的python- Kivy Screen Manager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Kivy构建一个多屏幕应用程序,我想使用ScreenManager在多个屏幕之间导航.我已经看到了有关如何在.kv文件中创建屏幕的示例和文档,但是我想知道如何在.py文件中创建屏幕.

I am building a multiple screen App with Kivy and I would like to use the ScreenManager to navigate between the multiple screens. I have seen examples and documentation for how to create the screens within a .kv file, but I want to know how to create them within the .py file.

  • 问题:当我如下图所示创建屏幕子类时,我的应用窗口返回黑屏.
  • 问题:正确的方法是在.py文件中创建Screen子类?

现在,我定义了两个Screen子类:"welcomeScreen"和"functionScreen".每个组件都由一个带有一些小部件的布局组成.

Right now I have two Screen subclasses defined: 'welcomeScreen', and 'functionScreen'. Each consists of a layout with some widgets.

kivy.require('1.9.1')
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
import kivy.uix.boxlayout
import kivy.uix.button
from kivy.uix.screenmanager import ScreenManager, Screen

class PanelBuilderApp(App):  # display the welcome screen

    def build(self):
        # Create the screen manager and add widgets to the base sm widget
        sm = kivy.uix.screenmanager.ScreenManager()
        sm.add_widget(Screen(name='welcomeScreen'))
        sm.add_widget(Screen(name='functionScreen'))
        # sm.current= 'welcomeScreen'
        return sm

class welcomeScreen(Screen): #welcomeScreen subclass

    def __init__(self, **kwargs): #constructor method
        super(welcomeScreen, self).__init__(**kwargs) #init parent
        welcomePage = FloatLayout()
        box = kivy.uix.boxlayout.BoxLayout(orientation='vertical', size_hint=(0.4, 0.3),
                                           padding=8, pos_hint={'top': 0.5, 'center_x': 0.5})

        welcomeLabel = Label(text='Hello and welcome to the Panel Builder version 1.0.\nApp by John Vorsten\nClick below to continue',
            halign= 'center', valign= 'center', size_hint= (0.4, 0.2), pos_hint= {'top': 1, 'center_x': 0.5})
        welcomeBox = kivy.uix.button.Button(text= 'Click to continue')
        welcomeBox.bind(on_press= self.callback)
        welcomeBox2 = kivy.uix.button.Button(text='not used')

        welcomePage.add_widget(welcomeLabel)
        box.add_widget(welcomeBox)
        box.add_widget(welcomeBox2)
        welcomePage.add_widget(box)
        self.add_widget(welcomePage)

    def callback(instance):
        print('The button has been pressed')
        sm.switch_to(Screen(name= 'functionScreen'))
        # sm.current = Screen(name= 'functionScreen')

class functionScreen(Screen):  #For later function navigation

    def __init__(self, **kwargs): #constructor method
        super(functionScreen, self).__init__(**kwargs) #init parent
        functionPage = kivy.uix.floatlayout.FloatLayout()

        functionLabel = Label(text='Welcome to the function page. Here you will choose what functions to use',
                              halign='center', valign='center', size_hint=(0.4,0.2), pox_hint={'top': 1, 'center_x': 0.5})

        functionPage.add_widget(functionLabel)
        self.add_widget(functionPage)

# sm.add_widget('Name') #Add more names later when you create more screens
# OR#
# for i in ScreenDirectory:
#     sm.add_widget(ScreenDirectory[i])

PanelBuilderApp().run()
if __name__ == '__main__':
    pass

我知道我可以将定义添加到.kv文件中,并且随着应用程序的增长,我可能会这样做.但是,我喜欢在学习如何使用kivy时保持露骨.

I understand I can add the definitions to a .kv file, and I will probably do this as the app grows. However, I like being explicit as I am learning how to use kivy.

推荐答案

我认为您认为使用 Screen(name ='welcomeScreen')就是在使用 welcomeScreen 是不正确的,如果您想使用 welcomeScreen ,则应直接使用它.

I think you think using Screen(name='welcomeScreen') you are using welcomeScreen but that is not true, if you want to use welcomeScreen you should use it directly.

另一方面,您有印刷错误,因此我已纠正,我建议您按照kivy教程进行操作,显然您必须具有扎实的OOP基础(我认为您没有此基础,因此您的任务是加强)./p>

On the other hand you have typographical errors so I have corrected, I recommend you follow the kivy tutorials, obviously you must have a solid OOP base (and I think you do not have it so your task is to reinforce).

import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen

class PanelBuilderApp(App):  # display the welcome screen
    def build(self):
        sm = ScreenManager()
        sm.add_widget(WelcomeScreen(name='welcomeScreen'))
        sm.add_widget(FunctionScreen(name='functionScreen'))
        return sm

class WelcomeScreen(Screen): #welcomeScreen subclass
    def __init__(self, **kwargs): #constructor method
        super(WelcomeScreen, self).__init__(**kwargs) #init parent
        welcomePage = FloatLayout()
        box = BoxLayout(orientation='vertical', size_hint=(0.4, 0.3),
                                           padding=8, pos_hint={'top': 0.5, 'center_x': 0.5})
        welcomeLabel = Label(text='Hello and welcome to the Panel Builder version 1.0.\nApp by John Vorsten\nClick below to continue',
            halign= 'center', valign= 'center', size_hint= (0.4, 0.2), pos_hint= {'top': 1, 'center_x': 0.5})
        welcomeBox = Button(text= 'Click to continue', on_press=self.callback)
        welcomeBox2 = Button(text='not used')

        welcomePage.add_widget(welcomeLabel)
        box.add_widget(welcomeBox)
        box.add_widget(welcomeBox2)
        welcomePage.add_widget(box)
        self.add_widget(welcomePage)

    def callback(self, instance):
        print('The button has been pressed')
        self.manager.current = 'functionScreen'

class FunctionScreen(Screen):  #For later function navigation
    def __init__(self, **kwargs): #constructor method
        super(FunctionScreen, self).__init__(**kwargs) #init parent
        functionPage = FloatLayout()
        functionLabel = Label(text='Welcome to the function page. Here you will choose what functions to use',
                              halign='center', valign='center', size_hint=(0.4,0.2), pos_hint={'top': 1, 'center_x': 0.5})
        functionPage.add_widget(functionLabel)
        self.add_widget(functionPage)

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

这篇关于.py文件中的python- Kivy Screen Manager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆