Interface centering

Peter Otten __peter__ at web.de
Sun Oct 24 05:11:10 EDT 2010


Jah_Alarm wrote:

> hi, I'm using Tkinter
> 
> cheers,
> 
> Alex
> 
> On 24 окт, 20:26, Chris Rebert <c... at rebertia.com> wrote:
>> On Sun, Oct 24, 2010 at 12:21 AM, Jah_Alarm <jah.al... at gmail.com> wrote:
>> > sorry 4 the sillu question.
>>
>> > I've designed a GUI. How can I center on the screen? (i.e. it's always
>> > launched in the center of the screen)
>>
>> Which GUI toolkit did you use?
>>
>> Cheers,
>> Chris

First hit googling for 'tkinter center' is

http://www.daniweb.com/forums/thread66181.html

With minor stylistic changes this becomes:

from Tkinter import *
 
def center_window(w, h):
    # get screen width and height
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    # calculate position x, y
    x = (ws//2) - (w//2) 
    y = (hs//2) - (h//2)
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))
 
root = Tk() 
center_window(500, 300) 
root.mainloop()

A good basic source for Tkinter programming is

http://infohost.nmt.edu/tcc/help/pubs/tkinter/

Geometry strings are explained here:

http://infohost.nmt.edu/tcc/help/pubs/tkinter/std-attrs.html#geometry

Peter




More information about the Python-list mailing list