Python 将GUI窗口显示在屏幕中间,需要用到win32api

来源:互联网 发布:java面向对象视频教程 编辑:程序博客网 时间:2024/06/10 07:52

使用tkinter画GUI时需要让窗口自动居中,以下便是实现方法:

# 通过win32api获取系统屏幕的分辨率def get_system_metrics():    from win32api import GetSystemMetrics    return GetSystemMetrics(0),GetSystemMetrics(1)# 传入窗口大小(分辨率)计算出窗口居中的位置def get_window_positons(width,height):    system_metrics =get_system_metrics()    window_x_position = (system_metrics[0] - width)//2    window_y_position = (system_metrics[1] - height) // 2    return window_x_position,window_y_position# 测试窗口def test_window():    import tkinter as tk    root = tk.Tk()    root_width = 300    root_height = 150    pos = get_window_positons(root_width, root_height)    root.geometry(f'{root_width}x{root_height}+{pos[0]}+{pos[1]}')      tk.mainloop()if __name__ == '__main__':    test_window()
阅读全文
0 0
原创粉丝点击