Paolopast
Paolopast

Reputation: 207

command function in tkinter does not work properly

I was trying to implement my first gui in python with tkinter. I have this function:

def clicked():
    if(b2["text"]=="Start Recording"):
        b2["text"]="Stop Recording"
    elif(b2["text"]=="Stop Recording"):
        b2["text"]="Start Recording"
    stt.start_speech_to_text("prove.txt")

where stt is a module that I implemented that uses google cloud speech to text library to convert the speach from the microphone into text.

I put this function into a button in this way:

b2 = Button(root, text='Start Recording',command=clicked)

but when I run the code and I press the button, the program starts with calling the start_speech_to_text function and only when I stop the execution of that function the button changes its label.

How can I solve this issue? Thanks

Upvotes: 0

Views: 60

Answers (1)

FrainBr33z3
FrainBr33z3

Reputation: 1105

you need to add a root.update_idletasks() or root.update() in your start_speech_to_text function. The mainloop doesn't update/redraw the widgets till it receives the control back from the start_speech_to_text function, and hence the update in the label can only be seen after the execution of the function

Upvotes: 2

Related Questions