Tkinter and centering

Alexander Schliep schliep at octopussy.mi.uni-koeln.de
Wed Apr 7 17:44:39 EDT 1999


Chad Netzer <chad at vision.arc.nasa.gov> writes:

> Well, I don't know if you can ever get the window width and height without
> first mapping it (displaying).  One option is to open it completely off the

I found a solution in Effective Tcl/Tk. You have to call update_idletasks()
which forces geometry calculations first and then you can move the window.

Here is some sample code I wrote for a splash screen. 

Alexander




class SplashScreen(Toplevel):
    """ 
        Subclass and override 'CreateWidgets()'
 
        In constructor of main window/application call      
        - S = SplashScreen(main=self)        (if caller is Toplevel) 
        - S = SplashScreen(main=self.master) (if caller is Frame) 
        - S.Destroy()  after you are done creating your widgets etc.
    """
 
    def __init__(self, master=None):
        Toplevel.__init__(self, master, relief=RAISED, borderwidth=5)
        self.main = master
        if self.main.master != None: # Why ?
            self.main.master.withdraw()
        self.main.withdraw()
        self.overrideredirect(1)
        self.CreateWidgets()
        self.after_idle(self.CenterOnScreen)
        self.update()
 
    def CenterOnScreen(self):
        self.update_idletasks()
        xmax = self.winfo_screenwidth()
        ymax = self.winfo_screenheight()
        x0 = (xmax - self.winfo_reqwidth()) / 2
        y0 = (ymax - self.winfo_reqheight()) / 2
        self.geometry("+%d+%d" % (x0, y0))
    
    def CreateWidgets(self):
	# Need to fill in here	
                
    def Destroy(self):
        self.main.update()
        self.main.deiconify()
        self.withdraw()
 



-- 
Alexander Schliep                    schliep at zpr.uni-koeln.de
ZPR/ZAIK                             Tel: +49-221-470-6011 (w)
University of Cologne                FAX: +49-221-470-5160
Weyertal 80                          http://www.zpr.uni-koeln.de/~schliep
50931 Cologne, Germany               Tel: +49-231-143083 (h)




More information about the Python-list mailing list