python之tkinter使用-窗口居中显示

 # 窗口居中显示
import tkinter as tk def set_win_center(root, curWidth='', curHight=''):
'''
设置窗口大小,并居中显示
:param root:主窗体实例
:param curWidth:窗口宽度,非必填,默认200
:param curHight:窗口高度,非必填,默认200
:return:无
'''
if not curWidth:
'''获取窗口宽度,默认200'''
curWidth = root.winfo_width()
if not curHight:
'''获取窗口高度,默认200'''
curHight = root.winfo_height()
# print(curWidth, curHight) # 获取屏幕宽度和高度
scn_w, scn_h = root.maxsize()
# print(scn_w, scn_h) # 计算中心坐标
cen_x = (scn_w - curWidth) / 2
cen_y = (scn_h - curHight) / 2
# print(cen_x, cen_y) # 设置窗口初始大小和位置
size_xy = '%dx%d+%d+%d' % (curWidth, curHight, cen_x, cen_y)
root.geometry(size_xy) root = tk.Tk()
root.resizable(False, False) # 窗口不可调整大小
root.title('窗口居中显示')
root.update() # 必须
set_win_center(root, 300, 300)
root.mainloop()

运行结果:

200 200
1924 1062
862.0 431.0

  

上一篇:Xamarin.Forms 未能找到路径“x:\platforms”的一部分


下一篇:IOS7,做为开发者,你需要知道的变更