469,356 Members | 2,055 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Statement orders

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?

Oct 2 '05 #1
5 971
Monu Agrawal wrote:
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>

Oct 2 '05 #2
Fredrik Lundh wrote:
Monu Agrawal wrote:
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>


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.
Oct 2 '05 #3
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+sJd01MZaTXX0RAm8wAJwJIr7c0mKmPdtoOoozvY 06T6hDAACfdcgV
BQGWxacx32Rg6eVMj1jZMjQ=
=vNvV
-----END PGP SIGNATURE-----

Oct 2 '05 #4
je****@unpythonic.net wrote:
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.
Oct 2 '05 #5
> 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)

iD8DBQFDQEkaJd01MZaTXX0RAnAzAJ92hHz2t6DgV6qQ7skXbD i+gVAyfgCghyo/
eUggQQ0eb2NIlONP9et6Opg=
=Fhrq
-----END PGP SIGNATURE-----

Oct 2 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jeff Mason | last post: by
3 posts views Thread by Chris | last post: by
6 posts views Thread by HeadScratcher | last post: by
6 posts views Thread by Twobridge | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.