簡體   English   中英

Kivy:異步更新標簽

[英]Kivy: update label asynchronous

我是 python 的新手,我正在用 kivy 編寫這個程序,以告訴我打開程序時它是哪個類,但我想更新我所在主題的標簽,但是當我嘗試使用while 循環,我不能運行任何其他東西。 這是我的python代碼:

from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.core.clipboard import Clipboard
from kivy.config import Config
from datetime import datetime
from kivy.app import App
import webbrowser
# ===========
Config.set('graphics', 'width', '1200')
Config.set('graphics', 'height', '600')
# ===========
with open("data.txt", "r") as a:
    All = a.readlines()
# ===========
ver = str.split(str.split(All[1], " = ")[1])
ver = ''.join(ver)
# ===========
Mon = str.split(str.split(All[3], " = ")[1])
Tue = str.split(str.split(All[4], " = ")[1])
Wed = str.split(str.split(All[5], " = ")[1])
Thu = str.split(str.split(All[6], " = ")[1])
Fri = str.split(str.split(All[7], " = ")[1])
Sat = str.split(str.split(All[8], " = ")[1])
# ===========
Math = str.split(str.split(All[10], " = ")[1])[0]
KNM = str.split(str.split(All[11], " = ")[1])[0]
Chem = str.split(str.split(All[12], " = ")[1])[0]
Lit = str.split(str.split(All[13], " = ")[1])[0]
Phys = str.split(str.split(All[14], " = ")[1])[0]
PE = str.split(str.split(All[15], " = ")[1])[0]
# ===========
Days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
Subject = ["Math", "KNM", "Chem", "Lit", "Phys", "PE"]
# ===========
Time = ["01:00 - 08:45",
        "08:45 - 09:35",
        "09:35 - 10:40",
        "10:40 - 11:30"]
# ===========
Trans = [
    "Math", "Toán",
    "KNM", "Kỹ năng mềm",
    "Chem", "Hóa học",
    "Lit", "Ngữ Văn",
    "Phys", "Vật lí",
    "PE", "Vovinam",
    "No class right now", "Không có lớp",
    "No class on Sunday", "Không học vào chủ nhật"]


def timecompare(time, clas):
    hr = time[:2]
    minu = time[-2:]
    mi = clas[:5]
    ma = clas[-5:]
    hrmin = mi[:2]
    minumin = mi[-2:]
    hrmax = ma[:2]
    minumax = ma[-2:]
    var = False
    if hrmin <= hr <= hrmax:
        if hr == hrmin and minumin <= minu:
            var = True
        if hr == hrmax and minu <= minumax:
            var = True
        if hrmin < hr < hrmax:
            var = True
    print(var)
    return var


def findclass(time, timelist):
    clas = 0
    for x in range(4):
        value = timecompare(time, timelist[x])
        if value is True:
            clas = x + 1
    return clas


def checksubject(timenow, timer):
    now = datetime.now()
    day = now.strftime("%a")
    subject = "No class right now"
    clas = findclass(timenow, timer)
    if clas != 0:
        for i in range(len(Days)):
            if day == Days[i]:
                subject = eval(Days[i])[clas - 1]
    if day == "Sun":
        subject = "No class on Sunday"
    return subject


class Main(Screen):
    ip = ObjectProperty(None)
    text = ObjectProperty(None)
    ver = ObjectProperty(None)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.checkclass()
        self.ver.text = "  version: " + ver

    def checkclass(self):
        now = datetime.now()
        nowtime = now.strftime("%H:%M")
        subject = Trans.index(checksubject(nowtime, Time)) + 1
        self.text.text = Trans[subject]
        nowtime = now.strftime("%H:%M")
        subject = checksubject(nowtime, Time)
        ip = None
        for x in range(len(Subject)):
            if subject == Subject[x]:
                ip = eval(Subject[x])
        if ip is not None:
            self.ip.text = ip
        else:
            self.ip.text = ""

    @staticmethod
    def joinclass():
        now = datetime.now()
        nowtime = now.strftime("%H:%M")
        subject = checksubject(nowtime, Time)
        ip = None
        for x in range(len(Subject)):
            if subject == Subject[x]:
                ip = eval(Subject[x])
        if ip is not None:
            webbrowser.open_new_tab(ip)

    def copy(self):
        super()
        self.checkclass()
        if self.ip.text != "":
            Clipboard.copy(self.ip.text)


class TKB(Screen):
    ver = ObjectProperty(None)

    a2 = ObjectProperty(None)
    a3 = ObjectProperty(None)
    a4 = ObjectProperty(None)
    a5 = ObjectProperty(None)
    a6 = ObjectProperty(None)
    a7 = ObjectProperty(None)

    b2 = ObjectProperty(None)
    b3 = ObjectProperty(None)
    b4 = ObjectProperty(None)
    b5 = ObjectProperty(None)
    b6 = ObjectProperty(None)
    b7 = ObjectProperty(None)

    c2 = ObjectProperty(None)
    c3 = ObjectProperty(None)
    c4 = ObjectProperty(None)
    c5 = ObjectProperty(None)
    c6 = ObjectProperty(None)
    c7 = ObjectProperty(None)

    d2 = ObjectProperty(None)
    d3 = ObjectProperty(None)
    d4 = ObjectProperty(None)
    d5 = ObjectProperty(None)
    d6 = ObjectProperty(None)
    d7 = ObjectProperty(None)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ver.text = "  version: " + ver

        self.a2.text = Trans[Trans.index(Mon[0]) + 1]
        self.b2.text = Trans[Trans.index(Mon[1]) + 1]
        self.c2.text = Trans[Trans.index(Mon[2]) + 1]
        self.d2.text = Trans[Trans.index(Mon[3]) + 1]

        self.a3.text = Trans[Trans.index(Tue[0]) + 1]
        self.b3.text = Trans[Trans.index(Tue[1]) + 1]
        self.c3.text = Trans[Trans.index(Tue[2]) + 1]
        self.d3.text = Trans[Trans.index(Tue[3]) + 1]

        self.a4.text = Trans[Trans.index(Wed[0]) + 1]
        self.b4.text = Trans[Trans.index(Wed[1]) + 1]
        self.c4.text = Trans[Trans.index(Wed[2]) + 1]
        self.d4.text = Trans[Trans.index(Wed[3]) + 1]

        self.a5.text = Trans[Trans.index(Thu[0]) + 1]
        self.b5.text = Trans[Trans.index(Thu[1]) + 1]
        self.c5.text = Trans[Trans.index(Thu[2]) + 1]
        self.d5.text = Trans[Trans.index(Thu[3]) + 1]

        self.a6.text = Trans[Trans.index(Fri[0]) + 1]
        self.b6.text = Trans[Trans.index(Fri[1]) + 1]
        self.c6.text = Trans[Trans.index(Fri[2]) + 1]
        self.d6.text = Trans[Trans.index(Fri[3]) + 1]

        self.a7.text = Trans[Trans.index(Sat[0]) + 1]
        self.b7.text = Trans[Trans.index(Sat[1]) + 1]
        self.c7.text = Trans[Trans.index(Sat[2]) + 1]
        self.d7.text = Trans[Trans.index(Sat[3]) + 1]


class ClassRemindApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(Main(name='Main'))
        sm.add_widget(TKB(name='TKB'))
        return sm


if __name__ == "__main__":
    ClassRemindApp().run()

kv 文件:

<Main>
    text: text
    ip: ip
    ver: ver
    canvas:
        Rectangle:
            pos: 0, root.height / 5 * 4 - 2
            size: root.width, 4
        Rectangle:
            pos: 0, root.height / 5 * 0.7 - 2
            size: root.width, 4
        Rectangle:
            pos: 0, root.height / 5 * 0.2 - 2
            size: root.width, 4
        Rectangle:
            pos: 0, root.height - 2
            size: root.width, 4
        Rectangle:
            pos: 0, 0 - 2
            size: root.width, 4
        Rectangle:
            pos: 0 - 2, 0
            size: 4, root.height
        Rectangle:
            pos: root.width - 2, 0
            size: 4, root.height
        Rectangle:
            pos: root.width / 6 - 2, root.height / 5 * 0.2 - 2
            size: 4, root.height
    GridLayout:
        cols: 1
        rows: 4
        GridLayout:
            cols: 2
            rows: 1
            Button:
                text: "Xem lớp"
                font_size: 30
                on_press: root.checkclass()
                size_hint: 0.2, 1
            Label:
                text: "Không có lớp"
                font_size: 30
                id: text
        GridLayout:
            cols: 2
            rows: 1
            size_hint: 1, 3.3
            Button:
                size_hint: 0.2, 1
                text: "Xem TKB"
                font_size: 30
                on_press:
                    root.manager.transition.direction = 'left'
                    root.manager.current = 'TKB'
            Button:
                text: "Mở link"
                font_size: root.width * root.height / 4000
                on_press: root.joinclass()
        GridLayout:
            cols: 2
            rows: 1
            size_hint: 1, 0.5
            Button:
                text: "Copy link"
                font_size: 30
                size_hint: 0.2, 1
                on_press: root.copy()
            Label:
                text: ""
                font_size : 30
                id: ip
                halign: 'left'
                valign: 'middle'
        Label:
            text: ""
            id: ver
            size_hint: 1, 0.2
            text_size: self.size
            halign: 'left'
            valign: 'middle'
<TKB>
    a2: a2
    a3: a3
    a4: a4
    a5: a5
    a6: a6
    a7: a7

    b2: b2
    b3: b3
    b4: b4
    b5: b5
    b6: b6
    b7: b7

    c2: c2
    c3: c3
    c4: c4
    c5: c5
    c6: c6
    c7: c7

    d2: d2
    d3: d3
    d4: d4
    d5: d5
    d6: d6
    d7: d7

    ver: ver
    canvas:
        Rectangle:
            pos: 0, root.height - 2
            size: root.width, 4
        Rectangle:
            pos: 0, 0 - 2
            size: root.width, 4
        Rectangle:
            pos: 0 - 2, 0
            size: 4, root.height
        Rectangle:
            pos: root.width - 2, 0
            size: 4, root.height
        Rectangle:
            pos: 0, root.height / 5 * 0.2 - 2
            size: root.width, 4
        Rectangle:
            pos: root.width / 6 - 2, root.height * 0.04
            size: 4, root.height
        Rectangle:
            pos: root.width / 6 - 2 + root.width * 0.14, root.height * 0.04
            size: 4, root.height * 0.16 * 2
        Rectangle:
            pos: root.width / 6 - 2 + root.width * 0.14 * 2, root.height * 0.04
            size: 4, root.height * 0.16 * 2
        Rectangle:
            pos: root.width / 6 - 2 + root.width * 0.14 * 3, root.height * 0.04
            size: 4, root.height * 0.16 * 2
        Rectangle:
            pos: root.width / 6 - 2 + root.width * 0.14 * 4, root.height * 0.04
            size: 4, root.height * 0.16 * 2
        Rectangle:
            pos: root.width / 6 - 2 + root.width * 0.14 * 5, root.height * 0.04
            size: 4, root.height * 0.16 * 2
        Rectangle:
            pos: root.width / 6 - 2 + root.width * 0.14 * 1, root.height * 0.04 + root.height * 0.16 * 3
            size: 4, root.height * 0.16 * 3
        Rectangle:
            pos: root.width / 6 - 2 + root.width * 0.14 * 2, root.height * 0.04 + root.height * 0.16 * 3
            size: 4, root.height * 0.16 * 3
        Rectangle:
            pos: root.width / 6 - 2 + root.width * 0.14 * 3, root.height * 0.04 + root.height * 0.16 * 3
            size: 4, root.height * 0.16 * 3
        Rectangle:
            pos: root.width / 6 - 2 + root.width * 0.14 * 4, root.height * 0.04 + root.height * 0.16 * 3
            size: 4, root.height * 0.16 * 3
        Rectangle:
            pos: root.width / 6 - 2 + root.width * 0.14 * 5, root.height * 0.04 + root.height * 0.16 * 3
            size: 4, root.height * 0.16 * 3
        Rectangle:
            pos: root.width / 6, root.height * 0.04 + root.height * 0.16 - 2
            size: root.width, 4
        Rectangle:
            pos: root.width / 6, root.height * 0.04 + root.height * 0.16 * 2 - 2
            size: root.width, 4
        Rectangle:
            pos: root.width / 6, root.height * 0.04 + root.height * 0.16 * 3 - 2
            size: root.width, 4
        Rectangle:
            pos: root.width / 6, root.height * 0.04 + root.height * 0.16 * 4 - 2
            size: root.width, 4
        Rectangle:
            pos: root.width / 6, root.height * 0.04 + root.height * 0.16 * 5 - 2
            size: root.width, 4
    GridLayout:
        cols: 1
        rows: 2
        GridLayout:
            cols: 2
            rows: 1
            Button:
                size_hint: 0.2, 1
                text: 'Quay về'
                font_size: 30
                on_press:
                    root.manager.transition.direction = 'right'
                    root.manager.current = 'Main'
            GridLayout:
                cols: 1
                rows: 3
                GridLayout:
                    size_hint: 1, 1.5
                    cols: 6
                    rows: 3
                    Label:
                        font_size: 30
                        text: 'Thứ 2'
                    Label:
                        font_size: 30
                        text: 'Thứ 3'
                    Label:
                        font_size: 30
                        text: 'Thứ 4'
                    Label:
                        font_size: 30
                        text: 'Thứ 5'
                    Label:
                        font_size: 30
                        text: 'Thứ 6'
                    Label:
                        font_size: 30
                        text: 'Thứ 7'
                    Label:
                        font_size: 20
                        id: a2
                        text: ''
                    Label:
                        font_size: 20
                        id: a3
                        text: ''
                    Label:
                        font_size: 20
                        id: a4
                        text: ''
                    Label:
                        font_size: 20
                        id: a5
                        text: ''
                    Label:
                        font_size: 20
                        id: a6
                        text: ''
                    Label:
                        font_size: 20
                        id: a7
                        text: ''

                    Label:
                        font_size: 20
                        id: b2
                        text: ''
                    Label:
                        font_size: 20
                        id: b3
                        text: ''
                    Label:
                        font_size: 20
                        id: b4
                        text: ''
                    Label:
                        font_size: 20
                        id: b5
                        text: ''
                    Label:
                        font_size: 20
                        id: b6
                        text: ''
                    Label:
                        font_size: 20
                        id: b7
                        text: ''

                Label:
                    size_hint: 1, 0.5
                    font_size: 30
                    text: 'Nghỉ 20p'

                GridLayout:
                    cols: 6
                    rows: 2
                    Label:
                        font_size: 20
                        id: c2
                        text: ''
                    Label:
                        font_size: 20
                        id: c3
                        text: ''
                    Label:
                        font_size: 20
                        id: c4
                        text: ''
                    Label:
                        font_size: 20
                        id: c5
                        text: ''
                    Label:
                        font_size: 20
                        id: c6
                        text: ''
                    Label:
                        font_size: 20
                        id: c7
                        text: ''

                    Label:
                        font_size: 20
                        id: d2
                        text: ''
                    Label:
                        font_size: 20
                        id: d3
                        text: ''
                    Label:
                        font_size: 20
                        id: d4
                        text: ''
                    Label:
                        font_size: 20
                        id: d5
                        text: ''
                    Label:
                        font_size: 20
                        id: d6
                        text: ''
                    Label:
                        font_size: 20
                        id: d7
                        text: ''
        Label:
            text: ""
            id: ver
            size_hint: 1, 0.04
            text_size: self.size
            halign: 'left'
            valign: 'middle'

和 data.txt 文件:

========================
ver = 2.0c
========================
Mon = Math Math KNM KNM
Tue = Chem Chem Phys Phys
Wed = Lit Lit Phys Phys
Thu = Chem Chem PE PE
Fri = Math Math Phys Phys
Sat = Chem Chem Math Math
========================
Math = https://meet.google.com/[link hidden]
KNM = https://meet.google.com/[link hidden]
Chem = https://meet.google.com/[link hidden]
Lit = https://meet.google.com/[link hidden]
Phys = https://meet.google.com/[link hidden]
PE = https://meet.google.com/[link hidden]
========================

您需要研究異步、線程和協處理。 Python 需要被告知你想要異步完成的事情。

暫無
暫無

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

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