python tkinter 窗口居中对齐

tkinter没有现成的窗口居中的功能,只能间接地算出来。

  1. from Tkinter import *
  2. def center_window(w=300, h=200):
  3.     # get screen width and height
  4.     ws = root.winfo_screenwidth()
  5.     hs = root.winfo_screenheight()
  6.     # calculate position x, y
  7.     x = (ws/2) - (w/2)   
  8.     y = (hs/2) - (h/2)
  9.     root.geometry('%dx%d+%d+%d' % (w, h, x, y))
  10. root = Tk()
  11. center_window(500, 300)
  12. root.mainloop()
标签:Python 发布于:2019-11-22 15:43:22