【问题标题】:How to access Kivy screens manager.<property> using Python如何访问 Kivy 屏幕管理器。<property> 使用 Python
【发布时间】:2016-05-10 22:59:41
【问题描述】:

背景:我一直在学习 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()

【问题讨论】:

    标签: android python screen kivy


    【解决方案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

    默认情况下,将显示第一个添加的屏幕。如果你想 显示另一个,只需设置“当前”属性。 sm.current = '秒'

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

    【讨论】:

    • 好的,我可以通过你这里的一些修改来让它工作。新问题/问题,但思路相同。 manager 没有 .transition 属性,所以当我使用 SlideTransition 时,它只会在每次转换时滑动一种方式。我知道它有一个“.transition”属性,但我的编译器再一次认为它不存在。
    • 我无法将 profile_button 绑定到传输方法声明上方,因为编译器认为传输此时不存在。最后,传递transit self,当绑定到按钮时传递的是按钮而不是屏幕。所以删除 self 并只使用 def transit(*args) 就像一个魅力。
    • 编辑:没关系,我终于可以访问过渡属性了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多