0

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

2
  • What do you mean by "seen outside the function"? This is just plain serial execution code, right? A "global" variable only exists inside a single running program. Commented Jan 31, 2019 at 14:58
  • 1
    It looks like you're using some kind of GUI library, so the answer is going to heavily depend on that library. It's possible that the function is running in some kind of event thread that's also used to render things to the screen, in which case you should decidedly not be doing time.sleep() in it, as you're robbing the event thread of the ability to respond to any events during that time. Commented Jan 31, 2019 at 15:01

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@MarcelloNeri happy it worked. You might want to add the tkinter tag (as tag) and the definitions of your StringVars (in the code) to your question -- it will help other people understand better.
1

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.

1 Comment

There might not be any need, but it does serve as documentation as to where this otherwise undefined variable comes from. (From another perspective, it raises the question of why the variable is being accessed globally, rather than being passed in as an argument.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.