I have two global variables (StringVar()) that need to be updated instantly inside a function, not at the end. This is my code.
def send_command_onLED():
global ReactionTime_HHHH
global ReactionTime_T
global string_time
global mode
global mode_sel
global command
global text_mode_sel
global go_text
if mode==0:
if command=='GUI':
text_mode_sel.set("") # bisogna fare in modo di far eseguire subito questi due comandi
go_text.set("GO!!!") #
elif command=='CMDL':
print("Ready, steady, go!!!")
ReactionTime_T = ''
ReactionTime_HHHH = ''
numcasuale = random.randint(10,20)
time.sleep(numcasuale)
ser.write("L1".encode())
t0 = time.time()
while(ReactionTime_T!='T'):
ReactionTime_T=ser.read(1).decode('utf-8')
t1 = time.time()
if t1-t0>70:
print("Error TIMEOUT!")
exit()
else:
ReactionTime_HHHH=ser.read(4).decode('utf-8')
ser.reset_input_buffer()
if command=='GUI':
if ReactionTime_HHHH=="FFFF":
string_time.set("You are drunk my friend!!!!")
else:
string_time.set(str(int(ReactionTime_HHHH, 16)) + " ms")
updateScore()
go_text.set("")
elif command=='CMDL':
if ReactionTime_HHHH=="FFFF":
print("\nYou are drunk my friend!!!!\n")
else:
print("\nNew score: " + str(int(ReactionTime_HHHH, 16)) + " ms\n")
mode=1
mode_sel=1
else:
mode_sel=0
if command=='CMDL':
print("Unable to execute this command, the LED is already ON! ")
The variables text_mode_sel and go_text have to be updated before time.sleep(), because they are textvariable of two Labels (I used tkinter) and their changes should be instantly seen outside the function before it ends. In other words, their changes should be displayed instantly. With my code, they are updated only when the function ends and changes before time.sleep() have no the expected effect. Is there a way to update these variables during the execution of a function??
time.sleep()in it, as you're robbing the event thread of the ability to respond to any events during that time.