473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

tkinter question

Hi

I have a tkinter question. In the following script the window will not
display until the script has finished executing. It appears at the same
time as the output "script finished". How can I make it appear
immediately, with the output "script finished" appearing 5 seconds
later.

cheers Dave

from Tkinter import *
import time

print "script started"

top = Tk()
F = Frame(top)
F.pack()
Hello = Label(F, text="hello world")
Hello.pack()

time.sleep(5)
print "script finished"

mainloop()

Mar 27 '06 #1
5 2599
On 27 Mar 2006 15:15:05 -0800, <li********@yahoo.com.au> wrote:
Hi

I have a tkinter question. In the following script the window will not
display until the script has finished executing. It appears at the same
time as the output "script finished". How can I make it appear
immediately, with the output "script finished" appearing 5 seconds
later.

cheers Dave

from Tkinter import *
import time

print "script started"

top = Tk()
F = Frame(top)
F.pack()
Hello = Label(F, text="hello world")
Hello.pack()
Here, the window is built with its contents, but the application is not
started until you've done a top.mainloop() (or just mainloop()). This is
required to be able to treat user requests. Until then, the window may
actually not show up (depends on the platform, in fact).
time.sleep(5)
Now you wait for 5 seconds, but the application is still not started.
print "script finished"
mainloop()


Now the application starts and begins to treat user requests.

What are you trying to do exactly? If you just want to automatically quit
the application after 5 seconds, here is a way to do it (untested):

--------------------------------------------
## The script starts exactly as yours do
from Tkinter import *

print "script started"

top = Tk()
F = Frame(top)
F.pack()
Hello = Label(F, text="hello world")
Hello.pack()

## Now, we'll register an event to call top.quit
## after 5 seconds (= 5000 milliseconds)
top.after(5000, top.quit)

## Now we start the main loop
top.mainloop()

## This should happen after top.quit() is
## executed by the event registered via after
print "script finished"
--------------------------------------------

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"
Mar 28 '06 #2
Ultimately what I am trying to is create a public computer session
manager.

1) User logs in and main application window is withdrawn (easy)
2) After (for example) 55 minutes - a warning window/dialoguebox
"session will end in 5 minutes"
3) With 30 seconds to go - a warning window/dialoguebox "session will
end in 30 seconds"
4) User logged out (easy)

I am having problems with steps 2 and 3
Either the window/dialoguebox will
(a) pause the program execution upon display waiting for user input
or (b) both warnings display together when application has finished.

What I want want is a window/dialogue box that will not halt
application logic, and will display immediately.

cheers David

Mar 29 '06 #3
(Please quote a part of the message you're replying to; I had problems
figuring out you were replying to my post...)

On 29 Mar 2006 14:08:02 -0800, <li********@yahoo.com.au> wrote:
Ultimately what I am trying to is create a public computer session
manager.

1) User logs in and main application window is withdrawn (easy)
2) After (for example) 55 minutes - a warning window/dialoguebox
"session will end in 5 minutes"
3) With 30 seconds to go - a warning window/dialoguebox "session will
end in 30 seconds"
4) User logged out (easy)

I am having problems with steps 2 and 3
Either the window/dialoguebox will
(a) pause the program execution upon display waiting for user input
or (b) both warnings display together when application has finished.

What I want want is a window/dialogue box that will not halt
application logic, and will display immediately.


But what does it do between steps 1 and 2? Is Python/Tkinter still in
control, or are you running some external program, or something else?

As for the dialogue box pausing the program execution waiting for user
input, you're probably using the "built-in" show* or ask* functions from
the tkMessageBox module that are designed to be modal. If you just create
a Toplevel and populate it with widgets, it will just display once the
control is returned to the Tkinter mainloop and no user input will be
needed. But the control *has* to return to the mainloop, or the window
won't show up. One acceptable workaround may be to call the "update"
method on the Toplevel, but it may depend on what you do between steps 1 &
2.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Mar 30 '06 #4
>If you just create
a Toplevel and populate it with widgets, it will just display once the
control is returned to the Tkinter mainloop and no user input will be
needed.


Thanks for explaining that. I borrowed the "after" method that you used
in your first reply. It seems to do what I want (except for
root.destroy) Though any suggestions on a more elegant solution
welcome.

cheers David
from Tkinter import *

def message1():
top1 = Tk()
F1 = Frame(top1)
F1.pack()
Hello1 = Label(F1, text="step 2 you will be logged off in 5
seconds")
Hello1.pack()
top1.after(3000, message2)

def message2():
top2 = Tk()
F2 = Frame(top2)
F2.pack()
Hello2 = Label(F2, text="step 3 you will be logged off in 2
seconds")
Hello2.pack()
top2.after(2000, goodbye)

def goodbye():
print "step 4 you are logged out"
root.destroy

root = Tk()
root.withdraw()
print "step 1 login window withdrawn"

message1()

root.mainloop()

Mar 30 '06 #5
On 30 Mar 2006 15:11:19 -0800, <li********@yahoo.com.au> wrote:
If you just create
a Toplevel and populate it with widgets, it will just display once the
control is returned to the Tkinter mainloop and no user input will be
needed.
Thanks for explaining that. I borrowed the "after" method that you used
in your first reply. It seems to do what I want (except for
root.destroy) Though any suggestions on a more elegant solution
welcome.

cheers David

[snip start of code] def goodbye():
print "step 4 you are logged out"
root.destroy


This is not a method call. To do what you want, write:
root.destroy()
Methods are first class objects in Python, so root.destroy is just the
method object for the destroy method applied to root. Written like that,
it just gets the object, but does not call it.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Mar 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Adonis | last post by:
I am creating some widgets by inheriting from Tkinter.Frame and populating the frame with whatever the widget will be then creating certain attributes/methods to be accessed later. My question is,...
25
by: BJörn Lindqvist | last post by:
See: http://www.wxpython.org/quotes.php. especially: "wxPython is the best and most mature cross-platform GUI toolkit, given a number of constraints. The only reason wxPython isn't the standard...
8
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have generally heard good things about wxPython. I...
4
by: Peter G Carswell | last post by:
Good Morning. I am new to Tkinter. I have been testing the installation of Tkinter through the python web site. The first two test steps give no errors, 'import _tkinter' and 'import Tkinter'....
1
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the...
0
by: Ron Adam | last post by:
I want to be able to easily create reusable shapes in Tkinter and be able to use them in mid level dialogs. So after some experimenting I've managed to get something to work. The following does...
2
by: Kevin Walzer | last post by:
I'm trying to decide whether I need threads in my Tkinter application or not. My app is a front end to a command-line tool; it feeds commands to the command-line program, then reads its output and...
7
by: Dick Moores | last post by:
In a couple of places recently I've seen Brent Welch's _Practical Programming in Tcl & Tk_ (<http://tinyurl.com/ynlk8b>) recommended for learning Tkinter well. So a couple of questions: 1) Is...
0
by: Guilherme Polo | last post by:
2008/5/10 Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net>: I will say no to the first question. Now about the second question.. there are these links you may find interesting: "An...
3
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.