如何使用Python访问Kivy屏幕管理器。<property>

时间:2016-05-10 22:59:41

标签: android python screen kivy

背景:我一直在学习Python - 通过它 - Kivy,通过制作应用程序。我一直在使用.kv文件和Builder.load_string方法来创建我的图形,但已决定尝试单独使用python,并将所有布局移动到python中。

问题:当我开始使用屏幕时,我无法将正确的代码绑定到按钮以使屏幕转换。当我写这行时,'self.manager.etc ...'自动完成向我显示了要使用的有效属性列表。

所以在'自我'之后。它表明我可以使用'经理',然后'经理'。它不认为屏幕的经理有“当前”或“过渡”属性。我一定搞砸了如何将屏幕连接到经理,但我无法理解如何。

class HomePage(Screen):
    def __init__(self, **kwargs):
        super(HomePage, self).__init__(**kwargs)

        layout = FloatLayout()

        notification = Label(text='upcoming: ....', font_size='16sp', size_hint=(0,0), pos_hint={'center_x':.5, 'top':0.9})
        layout.add_widget(notification)

        button_row=BoxLayout(size_hint_y=.1, spacing=20)

        profile_button=Label(text='Profile')
        button_row.add_widget(profile_button)

        layout.add_widget(button_row)
        self.add_widget(layout)

        def transit():
            self.manager.current = profile_button # <- this should work, right?

        profile_button.bind(on_press=transit)

class ScreenApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(HomePage(name='home'))
        return sm

if __name__ == "__main__":
    ScreenApp().run()

1 个答案:

答案 0 :(得分:1)

您应该在导入中添加转换

from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition

并在构建器中传递它

class ScreenApp(App):
    def build(self):
        sm = ScreenManager(transition=FadeTransition())

至于当前你应该添加第二个屏幕,给它一个名字,并使用该名称更改为该屏幕。来自https://github.com/kivy/kivy/blob/master/kivy/uix/screenmanager.py

  

默认情况下,将显示第一个添加的屏幕。如果你想   展示另一个,只需设置当前的&#39;属性。 sm.current =&#39; second&#39;

current也是一个字符串属性,你不能将它设置为标签

  

:attr:current是:class:~kivy.properties.StringProperty和   默认为无。

所以你的完整代码应该是

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition


class HomePage(Screen):
    def __init__(self, **kwargs):
        super(HomePage, self).__init__(**kwargs)

        layout = FloatLayout()

        notification = Label(text='upcoming: ....', font_size='16sp', size_hint=(0,0), pos_hint={'center_x':.5, 'top':0.9})
        layout.add_widget(notification)

        button_row=BoxLayout(size_hint_y=.1, spacing=20)

        profile_button=Button(text='Profile') # changed to a button
        button_row.add_widget(profile_button)
        profile_button.bind(on_press=self.transit) # moved here the bind action
        layout.add_widget(button_row)
        self.add_widget(layout)

    def transit(self, *args):
        # unintended to become a class method and reference to it with self
        print "ok"
        self.manager.current = "screen2"


class ProfilePage(Screen):
    def __init__(self, **kwargs):
        super(ProfilePage, self).__init__(**kwargs)
        layout = FloatLayout()
        labelP = Label(text="Profile Page")
        layout.add_widget(labelP)
        self.add_widget(layout)




class ScreenApp(App):
    def build(self):
        sm = ScreenManager(transition=FadeTransition())
        # create the first screen
        screen1 = HomePage(name='Home') #your home page
        screen2 = ProfilePage(name='screen2') # the second screen
        sm.add_widget(screen1)
        sm.add_widget(screen2)
        return sm

if __name__ == "__main__":
    ScreenApp().run()