簡體   English   中英

觸發屏幕管理器從 if 條件更改 Kivy 中的屏幕

[英]Triggering screenmanager to change screen in Kivy from an if conditional

我正在為用戶創建日期和時間選擇選項。 為此,我正在嘗試創建一個僅在所有微調器都選擇了值時才移動到下一個屏幕(單擊時)的按鈕。

為了讓程序識別何時單擊了微調器(日、小時和分鍾微調器),我在每個微調器的 .kv 代碼中為state分配了一個True值。 僅當所有微調器都具有state: True時,屏幕管理器才會從 screen_two 切換到 screen_three 。 單擊微調器(並選擇一個選項)時,我分配True

大多數代碼都按預期工作,除了我在下面顯示的 kv 代碼中的if語句。

我已經刪除了大部分代碼,只保留了相關的片段。

.kv 文件

<ScreenTwo>:
    FloatLayout:
        Button:
            background_color: 0, 0, 0, 1
            size: (400, 130)
            size_hint: (None, None)
            pos_hint: {'right': 0.6, 'center_y': 0}
            on_press:
                root.hours_checking() #this function converts the 12hr time to 24hr time

檢查每個微調器的狀態(各個微調器狀態由我分配的 ID 標識)。

 if day.state == hours.state == minutes.state == AmPm.state == 'True': \ root.manager.current = 'screen_three'
        Spinner:
            id: day
            size_hint: None, None
            size: 100, 44
            pos_hint: {'center': (.5, .5)}
            text: 'Day'
            values: 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'
            on_text:
                root.on_day_select(self.text) #this function sends back the selected day value in the spinner
                self.state: 'True'
        Spinner:
            id: hours
            size_hint: None, None
            size: 100, 44
            pos_hint: {'center': (.1, .5)}
            text: 'Hour'
            values: '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'
            on_text:
                root.on_hours_select(self.text) #this function sends back the selected hours value in the spinner
                self.state: 'True'
        Spinner:
            id: minutes
            size_hint: None, None
            size: 100, 44
            pos_hint: {'center': (.3, .5)}
            text: 'Minutes'
            values: '00', '15', '30', '45'
            on_text:
                root.on_minutes_select(self.text) #this function sends back the selected minutes value in the spinner
                self.state: 'True'
        Spinner:
            id: AmPm
            size_hint: None, None
            size: 100, 44
            pos_hint: {'center': (.4, .5)}
            text: 'AM/PM'
            values: 'a.m', 'p.m'
            on_text:
                root.on_AmPm_select(self.text) #this function sends back the selected day a.m/p.m value in the spinner
                self.state: 'True'

python 文件

class ScreenTwo(Screen):
    def on_day_select(self, text): #Function assigns selected day from spinner to a variable
        global day
        day = str(text)
    def on_hours_select(self, text): #Function assigns selected 12-hour from spinner to a variable
        global hours
        hours = int(text)
    def on_minutes_select(self, text): #Function assigns selected minute from spinner to a variable
        global minutes
        minutes = int(text)
    def on_AmPm_select(self,text): #Function assigns selected a.m/p.m from spinner to a variable
        global AmPm
        AmPm = str(text)
    def hours_checking(self): #Function converts 12hr time to 24hr time
        global AmPm
        global hours
        global minutes
        global day
        try:
            if 1 <= hours <= 11 and AmPm == 'a.m':
                pass
            elif 1 <= hours <= 12 and AmPm == 'p.m':
                hours += 12
            elif hours == 12 and AmPm == 'a.m':
                hours = 0
        except:
            print('error')
        else:
            print(day, hours, minutes)
    pass

運行此代碼並單擊按鈕時,我會收到 24 小時時間格式的 12 小時時間。 這意味着直到 kv 文件中的 screenmanager 部分(顯示在代碼塊中,以黃色部分突出顯示)之前的所有內容都可以完美運行。 我對 Kivy 和 OOP 很陌生,所以請幫我看看我到底哪里出錯了。

您可以通過創建Spinner的擴展來為您的目的創建自己的屬性,如下所示:

class MySpinner(Spinner):
    current_selection = BooleanProperty(False)

然后,將kv中的Spinner替換為MySpinner ,在您引用stateMySpinner的任何地方,將其替換為current_selection

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM