python- Kivy Screen Manager within .py file

你。 提交于 2021-01-28 05:41:49

问题


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.

  • Problem: When I create the screen subclasses as shown below, my app window returns a blank screen.
  • Question: What is the correct way to create the Screen subclasses within a .py file?

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

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.


回答1:


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.

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



来源:https://stackoverflow.com/questions/53386394/python-kivy-screen-manager-within-py-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!