Python Forum
Trying to clear a list automatically at certain times (guizero)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to clear a list automatically at certain times (guizero)
#1
Hello!

I'm a new coder and am trying to make guizero work for me. My intended program and what works:

- CSV files include the names and student IDs for each student in that class - different CSV per class period/list.
- GUI shows a text entry box for students to enter their IDs. Hit submit button and get a pop-up telling them any of the following:
1. I don't recognize this number (for numbers not in the CSV)
2. You've been marked (absent, tardy, present, or early) for (which hour) hour, (student's name)!

- GUI shows a text widget on the left side with every student name in the class.
- When a student successfully signs in with their ID number, their name disappears from the roster list in that text widget.
- When a student who has signed in has been marked tardy or late enough to be absent, their name still disappears, but reappears in a text widget on the right with their name and tar or abs.
- A clock appears at the bottom showing the current time.
- Every widget is set up to automatically update according to the current time. If it reaches the end of 1st hour, the roster switches to 2nd hour.

All of the above works like a charm. The problem is that when the class period switches over, the tar/abs list doesn't clear. It still lists who was absent or tardy from the previous class. If I log someone else in, their name is added to the list, but the previous class names still remain. No error is given. I have copied my code below:

 
from guizero import *
import time
import pandas as pd

stud_dict1 = {str(row[1]): row[0] for _, row in pd.read_csv('1st hour.csv').iterrows()}
stud_dict2 = {str(row[1]): row[0] for _, row in pd.read_csv('2nd hour.csv').iterrows()}
stud_dict3 = {str(row[1]): row[0] for _, row in pd.read_csv('3rd hour.csv').iterrows()}
stud_dict4 = {str(row[1]): row[0] for _, row in pd.read_csv('4th hour.csv').iterrows()}
stud_dict5 = {str(row[1]): row[0] for _, row in pd.read_csv('5th hour.csv').iterrows()}
stud_dict6 = {str(row[1]): row[0] for _, row in pd.read_csv('6th hour.csv').iterrows()}
stud_dict7 = {str(row[1]): row[0] for _, row in pd.read_csv('7th hour.csv').iterrows()}
stud_dict8 = {str(row[1]): row[0] for _, row in pd.read_csv('8th hour.csv').iterrows()}
stud_dict9 = {str(row[1]): row[0] for _, row in pd.read_csv('9th hour.csv').iterrows()}

t = time.localtime()
hr = int(time.strftime('%H', t))
mins = int(time.strftime('%M', t))
app = App(title="Attendance")
curtime = 'placeholder'
what_time = Text(app, text=curtime, align='bottom')
roster = stud_dict9
snames = roster.values()
snames = '\n'.join(snames)

cur_per = 9
names = []
atttitle = 'Yeah, that'
hr_title = Text(app, text=atttitle, size=30, align='top')

stud_att = {}


def update_lists():
    global roster
    global cur_per
    global stud_dict1
    global stud_dict2
    global stud_dict3
    global stud_dict4
    global stud_dict5
    global stud_dict6
    global stud_dict7
    global stud_dict8
    global stud_dict9
    global stud_att
    if cur_per == 1 and roster != stud_dict1:
        roster = stud_dict1
        stud_att.clear()
    elif cur_per == 2 and roster != stud_dict2:
        roster = stud_dict2
        stud_att.clear()
    elif cur_per == 3 and roster != stud_dict3:
        roster = stud_dict3
        stud_att.clear()
    elif cur_per == 4 and roster != stud_dict4:
        roster = stud_dict4
        stud_att.clear()
    elif cur_per == 5 and roster != stud_dict5:
        roster = stud_dict5
        stud_att.clear()
    elif cur_per == 6 and roster != stud_dict6:
        roster = stud_dict6
        stud_att.clear()
    elif cur_per == 7 and roster != stud_dict7:
        roster = stud_dict7
        stud_att.clear()
    elif cur_per == 8 and roster != stud_dict8:
        roster = stud_dict8
        stud_att.clear()
    elif cur_per == 9 and roster != stud_dict9:
        roster = stud_dict9
        stud_att.clear()


def cur_time():
    global hr
    global mins
    global curtime
    if int(hr) > 12:
        curtime_hr = str(int(hr)-12)
        ampm = "PM"
    else:
        curtime_hr = hr
        ampm = "AM"
    if int(mins) < 10:
        curtime_mins = '0'+str(int(mins))
    else:
        curtime_mins = mins
    curtime = f'{curtime_hr}:{curtime_mins} {ampm}'


def auto_refresh():
    global hr
    global mins
    global t
    global curtime
    global what_time
    global snames
    global tarabstext
    global tarabs
    t = time.localtime()
    hr = int(time.strftime('%H', t))
    mins = int(time.strftime('%M', t))
    cur_time()
    what_time.clear()
    what_time.append(curtime)
    hr_title.clear()
    hr_title.append(atttitle)
    update_lists()
    snames = roster.values()
    snames = '\n'.join(snames)
    students.clear()
    students.append(snames)
    tarabstext.clear()
    tarabstext.append(tarabs)


def current_per():
    global cur_per
    global roster
    global atttitle
    if (hr == 8 and mins < 59) or (hr == 9 and mins < 51):
        cur_per = 1
        atttitle = '1st hour'
        roster = stud_dict1
    elif (hr == 9 and mins < 59) or (hr == 10 and mins < 12):
        cur_per = 2
        atttitle = '2nd hour'
        roster = stud_dict2
    elif (hr == 10 and mins < 59) or (hr == 11 and mins < 11):
        cur_per = 3
        atttitle = '3rd hour'
        roster = stud_dict3
    elif (hr == 11 and mins < 59) or (hr == 12 and mins < 5):
        cur_per = 4
        atttitle = '4th hour'
        roster = stud_dict4
    elif hr == 12 and mins < 58:
        cur_per = 5
        atttitle = '5th hour'
        roster = stud_dict5
    elif (hr == 12 and mins < 59) or (hr == 13 and mins < 52):
        cur_per = 6
        atttitle = '6th hour'
        roster = stud_dict6
    elif (hr == 13 and mins < 59) or (hr == 14 and mins < 51):
        cur_per = 7
        atttitle = '7th hour'
        roster = stud_dict7
    elif (hr == 14 and mins < 59) or (hr == 15 and mins < 41):
        cur_per = 8
        atttitle = '8th hour'
        roster = stud_dict8
    else:
        cur_per = 9
        atttitle = 'All Hours'
        roster = stud_dict9


current_per()


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
    global roster
    if enter_id.value in roster:
        stud = roster.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 roster[enter_id.value]
        students.clear()
        snames = '\n'.join(roster.values())
        students.append(snames)
        enter_id.clear()
        if stud in stud_att:
            if stud_att[stud] == 'tardy' or 'absent':
                tarabstext.clear()
                tarabs = stud_att.items()
                tarabs = pd.DataFrame(stud_att.items(), columns=['Name', 'tar/abs'])
                tarabstext.append(tarabs)
    else:
        error("Um....", f"I don't recognize that number {enter_id.value}")


auto_refresh()
app.repeat(1000, auto_refresh)
app.repeat(1000, current_per)
submit.when_clicked = take_id

print(curtime)


app.display()
Any help is greatly appreciated.
Reply
#2
don´t do "from ... import *", that´s very bad code style

refactor your code and remove all "global",
using global is bad code style and is likely to cause problems

Use functions with parameters and let them return values to the calling code.
Reply
#3
Thanks! But I'm pretty new to this, so:

1. What would I do differently to import?
2. If I remove "global", PyCharm complains that the variable is from an outside source, or something like that. Do I just ignore that? Will it still work?
3. Can you give me an example of how I could change one of my functions to work the way you suggest, please? I learn best from examples.

I appreciate you so much!
Reply
#4
1) from Library import SpecificFunction --or-- if you actually need the entire library (which is highly unlikely) just import Library

2) No it will not work - BUT - globals are very very bad things

3) Passing stud_dict1 into auto_refresh() as follows auto_refresh(stud_dict1)

Now part of your problem of course is you have a lot of variables so perhaps making this into a class would be better

class studentstuff:
    self.stud_dict1 = {str(row[1]): row[0] for _, row in pd.read_csv('1st hour.csv').iterrows()}
    etc...

    def update_lists():
        if self.cur_per == 1 and self.roster != self.stud_dict1:
-- or basically something to that effect -- aka read up on classes and such
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Example of guizero ListBox jreebel 1 2,237 Oct-29-2020, 08:17 PM
Last Post: Larz60+
  guizero repeat command pdihawk 2 2,906 Sep-28-2020, 01:32 PM
Last Post: pdihawk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020