我无法清除奇异果的根

时间:2018-10-27 18:52:22

标签: python kivy

我希望清洁屏幕并根据某些按钮或情况带来新的小部件。第一页第二页等..

但是我要么将这些小部件带到了旧的部件上,要么遇到了我不理解的问题。

这是我的验证码

main.py

from kivy.app import App

class PageOne(App):
    def p_1(self):
        self.root.clear_widgets()
        PageTwo().run()

    def build(self, *args):
        pass

class PageTwo(App):
    def p_2(self):
        self.root.clear_widgets()
        PageOne().run()

    def build(self):
        pass

PageOne().run()

pageone.kv     :

FloatLayout:
    Label:
        size_hint: 1, 0.7
        pos_hint: {"top":1}
        text: "Page 1"
    Button:
        size_hint: 1, 0.3
        pos_hint: {"top": 0.3}
        text: "Open Page 2"
        on_press: app.p_1()

pagetwo.kv     :

FloatLayout:
    Label:
        size_hint: 1, 0.5
        pos_hint: {"top":1}
        text: "Page 2"
    Button:
        size_hint: 1, 0.3
        pos_hint: {"top": 0.3}
        text: "Open Page 1"
        on_press: app.p_2()

一些警告

[INFO   ] [MTD         ] Read event from </dev/input/event6>
[INFO   ] [Base        ] Start application main loop
[WARNING] [MTD         ] Unable to open device "/dev/input/event6". Please ensure you have the appropriate permissions.
[WARNING] [MTD         ] Unable to open device "/dev/input/event6". Please ensure you have the appropriate permissions.
[INFO   ] [Base        ] Leaving application in progress...
.
.
 AttributeError: 'PageOne' object has no attribute 'p_2'

我在做什么错!! 预先感谢

1 个答案:

答案 0 :(得分:1)

只有一个应用程序,如果要拥有多个视图,则应将多个Screen与ScreenManager一起使用,例如:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen


class PageOne(Screen):
    def p_1(self):
        self.manager.current = 'PageTwo'

class PageTwo(Screen):
    def p_2(self):
        self.manager.current = 'PageOne'

Builder.load_string('''
<PageOne>:
    FloatLayout:
        Label:
            size_hint: 1, 0.7
            pos_hint: {"top":1}
            text: "Page 1"
        Button:
            size_hint: 1, 0.3
            pos_hint: {"top": 0.3}
            text: "Open Page 2"
            on_press: root.p_1()

<PageTwo>:
    FloatLayout:
        Label:
            size_hint: 1, 0.5
            pos_hint: {"top":1}
            text: "Page 2"
        Button:
            size_hint: 1, 0.3
            pos_hint: {"top": 0.3}
            text: "Open Page 1"
            on_press: root.p_2()
''')

manager = ScreenManager()
manager.add_widget(PageOne(name="PageOne"))
manager.add_widget(PageTwo(name="PageTwo"))

class MainApp(App):
    def build(self):
        return manager

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