简体   繁体   中英

How to update instantly a global variable inside a function in Python 3?

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??

There is no need to mark text_mode_sel and go_text as global in this case.

And by the looks of it, only mode and mode_sel need to be global in this case. The rest is not being declared/assigned within this function so they'll work as expected without global.

As for not being updated straight away, it's hard to say what's happening with your code without knowing what go_text and text_mode_sel are but there is no delay within this code. I would expect that the library you are using might need a redraw command or so.

I assume you are using tkinter

If you are, you should call root.update() (or whatever your tkinter.Tk instance is called) to, as the name says, immediately update the screen. From __doc__ :

Enter event loop until all pending events have been processed by Tcl.

To have delayed execution, you may also want to look at root.after(time_in_ms, function) (again, your tkinter.Tk may have a different name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM