Sunday, July 12, 2009

Python tkinter tidbit: centred window

This little application simply creates a window and centres it on the screen (gist):


1 #! /usr/bin/env python3
2
3 from tkinter import *
4 from tkinter.ttk import *
5
6 def centred_win(root, width=400, height=300):
7 screen_width = root.winfo_screenwidth()
8 screen_height = root.winfo_screenheight()
9 x = (screen_width/2) - (width/2)
10 y = (screen_height/2) - (height/2)
11 root.geometry('%dx%d+%d+%d' % (width, height, x, y))
12
13 root = Tk()
14 centred_win(root, 500, 300)
15 root.mainloop()

No comments:

Post a Comment