您的位置:首页 > 编程语言 > Python开发

Python: tkinter窗口屏幕居中,设置窗口最大,最小尺寸

2017-07-21 15:17 1156 查看
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()  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: