Notifications
Clear all

[Solved] Order of Execution in Python?

3 Posts
2 Users
0 Likes
1,413 Views
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
Topic starter  

How can I control the order of execution of code within a function?

I have the following function:  

 

 

def listen():
    global speech
    speech = PocketClass.get_Speech()
    txtBox_1.delete(0.0,7.0)
    txtBox_1.insert(1.0,speech)   
    eS.say(speech) 

This function obtains speech from a microphone input and assigns it to the variable "result". 

I then clear a txtBox and insert the result into the txtBox.  

Then I call a routine to have the computer say that speech. 

However, when I run the program it always says the speech first, and then inserts the result into the text box.

Is there any way to force it to update the txtBox first? 

Why isn't it executing this code in the order it is written?

I'm having this problem with a lot of my routines.  I want to update things on the screen first and then call the eS.say method.  But it always calls the eS.say method first.

Is there any way I can force it to do things in the order I have them coded? 

Thanks

DroneBot Workshop Robotics Engineer
James


   
Quote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
Topic starter  

My apologies.   I figured out the problem. 

I'm using a Tkinter GUI window that contains the txtBox_1.   The code is actually updating the contents of the txtBox before it says the speech, but the Tkinter GUI window isn't being updated until after it leaves the function.  So visually it doesn't update until after the speech has been spoken.

The solution is as follows: 

 

def listen():
    global speech
    speech = PocketClass.get_Speech()
    txtBox_1.delete(0.0,7.0)
    txtBox_1.insert(1.0,speech)
    win_main.update_idletasks()    
    eS.say(speech)

I had to add a line to update the Tkinter window. 

 

win_main.update_idletasks()    

That updates the window immediately and the words appear in the text box before it actually says the words. 

So the code was executing in the correct order all along.   The Tkinter window just wasn't being updated until after the call to the listen() method finished. 

So the solution had to do with dynamically updating the Tkinter window using update_idletasks() inside the listen() function.  That was the solution. 

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1037
 

Boy, oh boy, learning something new here everyday!

I'm guessing the grass still needs cutting and the brakes and tires are waiting on you too!  🤣 

Unless you are doing those chores while the sun is up and doing all this coding and video production in the O-dark thirty time frame.  When are you sleeping?  No wonder you feel tired and worn down.  Take a break, we want you around for years to come!  🤪 🍺 

 

SteveG


   
ReplyQuote