如何在Python中自动刷新GUI文本?

时间:2019-07-11 20:51:19

标签: python-3.x

我正在为学生构建出勤应用程序。他们将输入学生证,并将其姓名从“缺席”列表中删除。如果它们很慢,则会将其名称添加到其他列表中,并显示它们很慢。该应用程序取决于时间和特定学生所在的班级-现在,如果它在第一小时的开始和结束之间,则应用程序的标题为1st Hour,并显示第一小时的花名册。如果我在第一个小时结束后重新初始化,它将给我第二个小时。我需要它在第一个小时结束时自动切换到第二个小时,而无需重新启动程序。

我到处都是互联网,包括Stack上的许多类似帖子,并且尝试了对我的问题有意义的每个答案,但是我尝试的所有操作都给了我一个错误,并且查找错误要么导致与我的需求无关或我不了解的事情(我只做了几周)。挫败感使我不得不发布自己的代码来寻求帮助。

from guizero import *

import time
import pandas as pd

t = time.localtime()
hr = int(time.strftime('%H', t))
mins = int(time.strftime('%M', t))
app = App(title="Attendance")


cur_per = 9
roster = ''
names = []

hr_title = Text(app)

stud_att = {}


def current_per():
    global cur_per
    global roster
    global hr_title
    if (hr == 8 and mins < 59) or (hr == 9 and mins < 25):
        cur_per = 1
        hr_title = Text(app, text="1st Hour", size=30, align='top')
        roster = '1st hour.csv'
    elif (hr == 9 and mins < 59) or (hr == 10 and mins < 12):
        cur_per = 2
        hr_title = Text(app, text="2nd Hour", size=30, align='top')
        roster = '2nd hour.csv'
    elif (hr == 10 and mins < 59) or (hr == 11 and mins < 11):
        cur_per = 3
        hr_title = Text(app, text="3rd Hour", size=30, align='top')
        roster = '3rd hour.csv'
    elif (hr == 11 and mins < 59) or (hr == 12 and mins < 5):
        cur_per = 4
        hr_title = Text(app, text="4th Hour", size=30, align='top')
        roster = '4th hour.csv'
    elif hr == 12 and mins < 58:
        cur_per = 5
        hr_title = Text(app, text="5th Hour", size=30, align='top')
        roster = '5th hour.csv'
    elif (hr == 12 and mins < 59) or (hr == 13 and mins < 52):
        cur_per = 6
        hr_title = Text(app, text="6th Hour", size=30, align='top')
        roster = '6th hour.csv'
    elif (hr == 13 and mins < 59) or (hr == 14 and mins < 51):
        cur_per = 7
        hr_title = Text(app, text="7th Hour", size=30, align='top')
        roster = '7th hour.csv'
    elif (hr == 14 and mins < 59) or (hr == 15 and mins < 41):
        cur_per = 8
        hr_title = Text(app, text="8th Hour", size=30, align='top')
        roster = '8th hour.csv'
    else:
        cur_per = 9
        hr_title = Text(app, text="9th Hour", size=30, align='top')
        roster = '9th hour.csv'


current_per()
stud_dict = {str(row[1]): row[0] for _, row in pd.read_csv(roster).iterrows()}

snames = stud_dict.values()
snames = '\n'.join(snames)


enter_id = TextBox(app)
submit = PushButton(app, text='Submit ID')
att = 'crazy'
nth_hr = 'ever'

students = Text(app, text=snames, size=12, align='left')
tarabs = '\n'. join(stud_att.items())
tarabstext = Text(app, text=tarabs, size=12, align='right')

def att_response():
    global att
    global nth_hr
    if hr == 8:
        nth_hr = '1st hour'
        if mins < 30:
            att = 'early'
        elif mins < 35:
            att = 'present'
        elif mins < 50:
            att = 'tardy'
        else:
            att = 'absent'
    elif hr == 9:
        if mins < 24:
            nth_hr = '1st hour'
            att = 'absent'
        elif mins < 29:
            nth_hr = '2nd hour'
            att = 'early'
        elif mins < 34:
            nth_hr = '2nd hour'
            att = 'present'
        elif mins < 49:
            nth_hr = '2nd hour'
            att = 'tardy'
        else:
            nth_hr = '2nd hour'
            att = 'absent'
    elif hr == 16:
        if mins < 11:
            nth_hr = '2nd hour'
            att = 'absent'
        elif mins < 16:
            nth_hr = '3rd hour'
            att = 'early'
        elif mins < 21:
            nth_hr = '3rd hour'
            att = 'present'
        elif mins < 36:
            nth_hr = '3rd hour'
            att = 'tardy'
        else:
            nth_hr = '3rd hour'
            att = 'absent'
    elif hr == 11:
        if mins < 10:
            nth_hr = '3rd hour'
            att = 'absent'
        elif mins < 15:
            nth_hr = '4th hour'
            att = 'early'
        elif mins < 20:
            nth_hr = '4th hour'
            att = 'present'
        elif mins < 35:
            nth_hr = '4th hour'
            att = 'tardy'
        else:
            nth_hr = '4th hour'
            att = 'absent'
    elif hr == 12:
        if mins < 4:
            nth_hr = '4th hour'
            att = 'absent'
        elif mins < 9:
            nth_hr = '5th hour'
            att = 'early'
        elif mins < 14:
            nth_hr = '5th hour'
            att = 'present'
        elif mins < 29:
            nth_hr = '5th hour'
            att = 'tardy'
        elif mins < 58:
            nth_hr = '5th hour'
            att = 'absent'
        else:
            nth_hr = '6th hour'
            att = 'early'
    elif hr == 13:
        if mins < 3:
            nth_hr = '6th hour'
            att = 'early'
        elif mins < 8:
            nth_hr = '6th hour'
            att = 'present'
        elif mins < 23:
            nth_hr = '6th hour'
            att = 'tardy'
        elif mins < 52:
            nth_hr = '6th hour'
            att = 'absent'
        elif mins < 57:
            nth_hr = '7th hour'
            att = 'early'
        else:
            nth_hr = '7th hour'
            att = 'present'
    elif hr == 14:
        if mins < 2:
            nth_hr = '7th hour'
            att = 'present'
        elif mins < 17:
            nth_hr = '7th hour'
            att = 'tardy'
        elif mins < 46:
            nth_hr = '7th hour'
            att = 'absent'
        elif mins < 51:
            nth_hr = '8th hour'
            att = 'early'
        elif mins < 56:
            nth_hr = '8th hour'
            att = 'present'
        else:
            nth_hr = '8th hour'
            att = 'tardy'
    elif hr == 15:
        if mins < 11:
            nth_hr = '8th hour'
            att = 'tardy'
        elif mins < 40:
            nth_hr = '8th hour'
            att = 'absent'


def take_id():
    global stud
    global snames
    global students
    global tarabs
    global tarabstext
    if enter_id.value in stud_dict:
        stud = stud_dict.get(enter_id.value)
        att_response()
        info("Yay! You did a thing!", f"You've been marked {att} for {nth_hr}, {stud}!")
        stud_att[stud] = att
        del stud_dict[enter_id.value]
        students.destroy()
        snames = '\n'.join(stud_dict.values())
        students = Text(app, text=snames, size=12, align='left')
        enter_id.clear()
        if stud in stud_att:
            if stud_att[stud] == 'tardy' or 'absent':
                tarabstext.destroy()
                tarabs = stud_att.items()
                tarabs = pd.DataFrame(stud_att.items(), columns=['Name', '?'])
                tarabstext = Text(app, text=tarabs, size=12, align='right')
    else:
        error("Um....", f"I don't recognize that number {enter_id.value}")


submit.when_clicked = take_id


app.display()

同样,除了时间/课程时间的自动更新以外,在所有方面的输出都完全符合要求。我同意可能有更高效/更清洁的方法来完成我已经做过的事情-我很想了解这一点-但我现在的首要任务是弄清楚难题的最后一部分,以便我继续开学前去做其他事情。感谢您的帮助!

0 个答案:

没有答案