如何修复功能消失后无法显示的画面?

时间:2019-04-17 23:47:53

标签: python kivy

我要保存一个对象,然后移至下一个屏幕。我拥有的代码仅保存对象,并保留在同一页面上。我认为我的主要问题是能够连接我的kv代码和python代码。我认为程序不知道ScreenTwo的变量是什么。我该如何解决? 这是可能会影响它的代码:

class MainScreen(Screen):
    pass

class ScreenOne(Screen):
    pass

class ScreenTwo(Screen):
    pass

class ScreenManagement(ScreenManager):
    main_screen = ObjectProperty(None)
    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)

presentation = Builder.load_file("StreakStar.kv")

class MainApp(App):
    def build(self): # build() returns an instance
        self.store = JsonStore("streak.json")

        return presentation

    def change_screen(self, *args):
        ScreenManagement.current = "ScreenTwo"

    def display_btn(self):
        obj = self.root.get_screen('one')

        for key in self.store:
            streak_button = Button(text=key)
            obj.ids.streak_zone.add_widget(streak_button)


    # creates the Streak object
    def create(self):
        obj = self.root.get_screen('one') # get info from ScreenOne
        self.streak = Streak(obj.ids.action_entry.text, obj.ids.streak_entry.text,
                            obj.ids.day_entry.text, obj.ids.hour_entry.text,
                            obj.ids.minute_entry.text)

        empty_error = "Make sure to fill out all boxes!"

        popup = Popup(title="Not filled", content=Label(text=empty_error),
                     size_hint=(None, None), size=(300, 100))



        # error handling and calculating total seconds
        parsed = False
        try:
            total = ((int(self.streak.day) * 86400) + (int(self.streak.hour) * 3600) +
                    (int(self.streak.minute) * 60)) # convert into seconds
            parsed = True
            # delete later and replace with return
            print("[seconds:", total,']' , "[action:", self.streak.action,']',
                 "[action number:", self.streak.action_num,']')

            self.store.put(self.streak.action, action=self.streak.action,
                          action_num=self.streak.action_num, seconds=total,
                          score=self.streak.score)

            self.change_screen(self)

        except ValueError as error:
            popup.open()

.kv代码:

ScreenManagement:
    id: screen_manager

    main_screen: main_screen
    screen_one: screen_one
    screen_two: screen_two

    transition: SlideTransition()

    MainScreen:
        id: main_screen
        name: "main"
        manager: screen_manager

    ScreenOne:
        id: screen_one
        name: "one"
        manager: screen_manager

    ScreenTwo:
        id: screen_two
        name: "two"
        manager: screen_manager

...

        Button: # this button is apart of ScreenOne
            text: "Add"
            size: 50, 25
            size_hint: None, None
            font_size: 18
            on_press: app.create()

<ScreenTwo>
    id: screen_two
    name: "two"
    GridLayout:
        cols: 2
        rows: 1
        BoxLayout:
            id: streak_zone
            orientation: 'vertical'
        AnchorLayout:
            anchor_x: "center"
            anchor_y: "center"
            Label:
                id: high_lable
                text: "Highest Streak: "
                size_hint: None, None
                font_size: 20

2 个答案:

答案 0 :(得分:0)

切换屏幕

调用方法create()之后,如果要转到ScreenTwo,则将ScreenManagement.current = "ScreenTwo"替换为self.root.current = 'two'

摘要

def change_screen(self, *args):
    self.root.current = "two"

在另一个屏幕中访问属性

由于使用了ObjectProperty来链接屏幕,因此可以通过两种方法访问ScreenTwo中的变量。

方法1

使用self.root.screen_one.ids.id_nameself.root.screen_two.ids.id_name

摘要

self.root.screen_one.ids.action_entry.text
self.root.screen_one.ids.streak_entry.text
self.root.screen_one.ids.day_entry.text
self.root.screen_one.ids.hour_entry.text
self.root.screen_one.ids.minute_entry.text

self.root.screen_two.ids.streak_zone
self.root.screen_two.ids.high_lable.text

方法2

您已经在使用其中之一,即obj = self.root.get_screen('one')

答案 1 :(得分:0)

您正在从类change_screen呼叫ScreenManagement.current = "ScreenTwo"。由于您的经理是您的根,请尝试self.root.current