如何在不使用.kv文件的情况下使用屏幕管理器

时间:2019-06-24 06:44:13

标签: python-3.x kivy

我试图创建多个屏幕(即尝试使用屏幕管理器)而不使用.kv文件,即全部位于.py文件中,但是我无法创建它。请帮帮我。基本上我想知道如何不使用屏幕管理器。 Kv文件

我曾经尝试做到,但是我做不到

# kivy code 
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen

class SwitchApp(App): 

    # define build function 
    def build(self): 

        # Create the manager
        sm = ScreenManager()

        # Add few screens
        for i in range(4):
            screen = Screen(name='Title %d' % i)
            sm.add_widget(screen)

        # By default, the first screen added into the ScreenManager will be
        # displayed. You can then change to another screen.

        # Let's display the screen named 'Title 2'
        # A transition will automatically be used.
        sm.current = 'Title 2'

        return sm


# Run the kivy app 
if __name__ == '__main__': 
     SwitchApp().run()

1 个答案:

答案 0 :(得分:-1)

我为每个屏幕添加了小部件

(screen.add_widget(Label(text =“%d”%i)))

我选择了要显示的屏幕1,您可以在它们之间切换

    sm.current = 'Title 1'

猕猴桃代码

从kivy.app导入应用

从kivy.uix.screenmanager导入ScreenManager,屏幕

从kivy.uix.label导入标签

SwitchApp(App)类:

# define build function

def build(self):

    # Create the manager

    sm = ScreenManager()

    # Add few screens

    for i in range(4):

        screen = Screen(name='Title %d' % i)

        screen.add_widget(Label(text="%d" % i))

        sm.add_widget(screen)




    sm.current = 'Title 1'

    return sm

运行Kivy应用程序

如果名称 =='主要':

 SwitchApp().run()