Statement orders

M

Monu Agrawal

Hi I am making a gui based tool. When user preses a perticular button I
am running a heavy command, before this I want to say user to wait with
a image showing infront of her.
My code is like:
def loadData(self):
top=Toplevel(self.parent)
top.focus_set()
self.parent.wm_title("Loading Data...")

os.system('a heavy command')
os.system('another heavy command)
top.destroy()

Now when I apply it, it first runs the os.system commands then it shows
the top level.
I tried with putting sleep just before os.system() it didn't help.
Is some statement reordering going on, or I am missing something?
 
F

Fredrik Lundh

Monu said:
Hi I am making a gui based tool. When user preses a perticular button I
am running a heavy command, before this I want to say user to wait with
a image showing infront of her.

My code is like:

def loadData(self):
top=Toplevel(self.parent)
top.focus_set()
self.parent.wm_title("Loading Data...")

+ top.update() # flush the event queue
os.system('a heavy command')
os.system('another heavy command)
top.destroy()

</F>
 
D

David Murmann

Fredrik said:
+ top.update() # flush the event queue


</F>

I had a very similar problem and adding an update call solved it, but
in some tk documentation i read that one should be careful with adding
update calls to callbacks and prefer update_idletasks. in my case the
update method is even called one time before the mainloop is entered,
yet everything seems to work fine, so exactly when is it dangerous to
call update? (or is it dangerous at all?)

David.
 
J

jepler

Here's one case where it's bad to call update.

def perform_longrunning_calculation():
time.sleep(1)
app.update()
time.sleep(1)

suppose you kick this off with a keybinding, such as:
app.bind("c", lambda e: perform_longrunning_calculation())
the calculation takes 2 seconds, and after 1 second update() is called,
possibly to make sure that the user interface is repainted.

But imagine that in the first second, "c" has been pressed. That event will
be handled, and perform_longrunning_calculation will be entered *again*,
recursively. The nesting looks something like this:
app's mainloop
handling keypress
perform_longrunning_calculation()
app.update()
handling keypress
perform_longrunning_calculation()
by calling update_idletasks instead of update, the keypress event is not
handled, so it's impossible to enter perform_longrunning_calculation a
second time.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDQC+sJd01MZaTXX0RAm8wAJwJIr7c0mKmPdtoOoozvY06T6hDAACfdcgV
BQGWxacx32Rg6eVMj1jZMjQ=
=vNvV
-----END PGP SIGNATURE-----
 
D

David Murmann

Here's one case where it's bad to call update.

def perform_longrunning_calculation():
time.sleep(1)
app.update()
time.sleep(1)

would it be advisable to guard against this with something like this?

def perform_longrunning_calculation():
if not app.busy:
app.busy = 1
time.sleep(1)
app.update()
time.sleep(1)
app.busy = 0

or does this have flaws i'm not seeing?

David.
 
J

jepler

would it be advisable to guard against this with something like this?
def perform_longrunning_calculation():
if not app.busy:
app.busy = 1
[...]
By using that kind of construct, instead of using update_idletasks(),
you force all code to be aware of and manage the app.busy flag.

Another difference is that with the original code changed to use
update_idletasks(), perform_longrunning_calculation *will* be called
twice, the second time after the first one completes. With your code,
it will be called once if the second keypress comes within one second, and
twice if called after one second is up. (though a second update before setting
busy back to false will make it be called only once)

So in also depends on what you want your application to do in these cases.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDQEkaJd01MZaTXX0RAnAzAJ92hHz2t6DgV6qQ7skXbDi+gVAyfgCghyo/
eUggQQ0eb2NIlONP9et6Opg=
=Fhrq
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top