1

I am working on a program in Python with guizero (running on a Raspberry Pi) with witch users can select drinks and identify with an RFID Chip. The transaction is then stored in a mariadb on a remote device.

The idea is that users selects a drink, then the screen changes for 10 Seconds to a promt where he is asked to authenticate with his RFID chip . If he does not, the software should return to the main screen.

So far, everything works well but I am having trouble with the GUI. Since the 10 Second-Scan-Period is a while-Loop, it freezes the whole gui and does not show the promt, and leaving the user clueless of what he has to do.

What I've tried:

  • I used a Thread-Object to call the scan function, but that resulted in the promt disappearing very fast.
  • I tried callbacks but that meant to freeze my gui anyway
  • I tried the repeat-method for the App-Object and removing the while-loop, but that would mean that the system is scanning non-stop, which is not intended.

Here's a little example-code which describes my program in a very simple way:

from guizero import App, Text, PushButton
explanation = "To scan your chip, please click on the button."

def scan():
    text.value = "Scanning...."
    #set the endtime to ten seconds from now
    endtime = time.time() + 10
    
    #repeat for ten seconds
    while time.time() < endtime:
        print("Scanning for RFID")
    print("End of scan")
    text.value = explanation
        
        
app = App("Funny Title Here")

#initial welcome text
text = Text(app,text=explanation)

#if button is clicked, change the text and scan for ten seconds
button = PushButton(app, command=scan, text="Scan")

app.display()

I know that one of the methods I tried is the right one, but I seem to lack the needed logic. So my question is: How can I achieve, that the gui updates after the user pushed a button, starts to scan for RFID cards, and after 10 seconds stops and returns to the original view?

Thanks

Maerty87
  • 11
  • 1

1 Answers1

0

I found a solution! Here's how I solved it: the idea is, to call the scan-function every 100 milliseconds. When the user clicks on the button, a boolean is set to true and the scan-function does something. if the boolean is false, then nothing happens.

import time
from guizero import App, Text, PushButton
explanation = "To scan your chip, please click on the button."
isScan=False
counter = 0
endtime = 0

def scan():
    global isScan
    if isScan:
        global endtime
        global counter

        #repeat for ten seconds
        if time.time()<endtime:
            print(f"{counter}")
            counter += 1
        else:
            isScan=False
            print("End of scan")
            counter = 0 
            text.value = explanation
        
            
def scanScreen():
    global isScan
    global endtime
    text.value = "Scanning...."
    endtime = time.time()+10
    isScan = True
    
    
app = App("Funny Title Here")

#initial welcome text
text = Text(app,text=explanation)

#if button is clicked, change the text and scan for ten seconds
button = PushButton(app, command=scanScreen, text="Scan")
app.repeat(100,scan)
app.display()
Maerty87
  • 11
  • 1