Tk and focusing windows
Matthew Dixon Cowles
matt at mondoinfo.com
Wed Oct 24 13:43:08 EDT 2001
On Wed, 24 Oct 2001 07:16:06 GMT, Graham Ashton <graz at mindless.com>
wrote:
>I've got an application which opens two Tk windows as soon as it starts
>up; the main window and a progress dialog.
>
>I want the dialog to be displayed after the main window (so the main
>window can't cover it up), but the way my code is currently structured I
>call mainloop() immediately after creating the dialog. The code for the
>main window is done first.
>
>The main window is instantiated with "root = Tk()" before anything else
>happens, and the dialog is created with Toplevel().
Graham,
Tkinter doesn't make it easy to get things displayed attractively
during startup. I find that getting things to look the way I want
often requires figuring out the right places to put calls to
update_idletasks(). I'll append a small example that may help.
Of course, once you've got the right incantation for your window
manager, there's no guarantee that another window manager will do just
the same thing in response to the same requests.
Regards,
Matt
#!/usr/local/bin/python
from Tkinter import *
import time
class mainWin:
def __init__(self,root):
self.root=root
self.progressWin=None
self.createWidgets()
self.root.update_idletasks()
self.root.after_idle(self.doTimeConsumingSetup)
return None
def createWidgets(self):
l=Label(self.root,height=10,width=40,text="Main")
l.pack()
return None
def doTimeConsumingSetup(self):
self.showProgressWin()
time.sleep(5)
self.hideProgressWin()
return None
def showProgressWin(self):
self.progressWin=Toplevel()
l=Label(self.progressWin,text="Please wait")
l.pack()
self.root.update_idletasks()
return None
def hideProgressWin(self):
self.progressWin.destroy()
return None
def main():
root=Tk()
mainWin(root)
root.mainloop()
return None
if __name__=='__main__':
main()
More information about the Python-list
mailing list