1. 程式人生 > >Python: tkinter視窗螢幕居中,設定視窗最大,最小尺寸

Python: tkinter視窗螢幕居中,設定視窗最大,最小尺寸

轉自:http://blog.csdn.net/yao_yu_126/article/details/23717355

#!/usr/bin/env python  
#coding=utf-8  
''''' 
    視窗螢幕居中,設定視窗最大,最小尺寸... 
    版權所有 2014 yao_yu (http://blog.csdn.net/yao_yu_126) 
    本程式碼以MIT許可協議釋出 
    2014-04-15  建立 
'''  
  
import tkinter  as tk  
from tkinter    import ttk  
  
def get_screen_size(window):  
    return window.winfo_screenwidth(),window.winfo_screenheight()  
  
def get_window_size(window):  
    return window.winfo_reqwidth(),window.winfo_reqheight()  
  
def center_window(root, width, height):  
    screenwidth = root.winfo_screenwidth()  
    screenheight = root.winfo_screenheight()  
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)  
    print(size)  
    root.geometry(size)  
  
root = tk.Tk()  
root.title('測試視窗')  
center_window(root, 300, 240)  
root.maxsize(600, 400)  
root.minsize(300, 240)  
ttk.Label(root, relief = tk.FLAT, text = '螢幕大小(%sx%s)\n視窗大小(%sx%s)' % (get_screen_size(root) + get_window_size(root))).pack(expand = tk.YES)  
tk.mainloop()