您的位置:首页技术文章
文章详情页

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

【字号: 日期:2022-08-04 10:17:04浏览:16作者:猪猪
导读:我就废话不多说了。大家直接看代码吧!#!/usr/bin/env python#coding=utf-8’’’ 窗口屏幕居中,设置窗口最大,最小尺寸... 版权所有 2014 yao_yu (http://blog.csdn.net/yao_yu_126) 本代码以MIT许可协议发布 201...

我就废话不多说了。大家直接看代码吧!

#!/usr/bin/env python#coding=utf-8’’’ 窗口屏幕居中,设置窗口最大,最小尺寸... 版权所有 2014 yao_yu (http://blog.csdn.net/yao_yu_126) 本代码以MIT许可协议发布 2014-04-15 创建’’’ import tkinter as tkfrom 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()

补充知识:关于tkinter几个需要用到的窗口设置

设置窗口的大小和位置

geometry(widthxheight+x+y)

窗口的长宽分别为width和height;

窗口到主窗口的间距为 x和 y ;

注意可以使用减号,例如 10x10-10-10 代表10*10大小的窗口出现在右下角,但是不能直接使x或y为负值,然后带入 wxh+x+y;同时这个格式里不能有空格。

当没有参数时,用此方法能返回当前的尺寸位置参数。

# -*- coding:utf-8 -*-from tkinter import *root = Tk()width, height, padx, pady = 800, 600, 40, 300root.geometry(’%dx%d-%d+%d’ % (width, height, padx, pady))

设置窗口样式、透明和全屏

-toolwindow 可设置窗口为工具栏样式;

-alpha 可设置透明度,0完全透明,1不透明。这里透明是窗口内的所有内容,不仅是窗体,所以要特别小心一个完全透明的窗口!

-fullscreen 设置全屏 注意前面的短横杠(-) 不能少

-topmost 设置窗口置顶。两个同时被置顶的窗口为同级(能互相遮盖),但他们都能同时遮盖住没有被设置为置顶的窗口。

root.attributes(’-toolwindow’, False, ’-alpha’, 0.9, ’-fullscreen’, True, ’-topmost’, True)

去掉标题栏

去掉窗口的框架,脱离windows窗口管理。所以此时你也不能拖动它。并且这个窗口也不会出现在任务栏。

root.overrideredirect(True)

思考题:一个全屏、置顶、透明、去标题栏的窗口将意味着什么?

以上这篇Python: tkinter窗口屏幕居中,设置窗口最大,最小尺寸实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持好吧啦网。

标签: Python 编程