如何在功能后修复屏幕未显示?

问题描述 投票:0回答:2

我想保存一个对象,然后移动到下一个屏幕。我只保存了对象并保留在同一页面上的代码。我认为我的主要问题是能够连接我的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

python kivy
2个回答
0
投票

Switching Screen

在调用方法create()之后,如果你想去ScreenTwo然后用ScreenManagement.current = "ScreenTwo"替换self.root.current = 'two'

片段

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

Accessing Attributes in another screen

由于您已使用ObjectProperties链接屏幕,因此您可以使用两种方法访问ScreenTwo中的变量。

方法1

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

Snippets

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


0
投票

你是从班级change_screen那里打电话给ScreenManagement.current = "ScreenTwo"的。既然你的经理是你的根,请尝试self.root.current

© www.soinside.com 2019 - 2024. All rights reserved.