How do I get a windows current size using Tkinter?
Use the following universal widget methods (where w is a widget):
w.winfo_height()
w.winfo_width()
You can also use the following:
w.winfo_reqheight()
w.winfo_reqwidth()
Read about universal widget methods.
#Import the required libraries
from tkinter import *
#Create an instance of Tkinter Frame
win = Tk()
#Set the geometry
win.geometry("700x350")
#Set the default color of the window
win.config(bg='#aad5df')
#Create a Label to display the text
label=Label(win, text= "Hello World!",font= ('Helvetica 18 bold'), background= 'white', foreground='purple1')
label.pack(pady = 50)
win.update()
#Return and print the width of label widget
width = label.winfo_width()
print("The width of the label is:", width, "pixels")
win.mainloop()
获得窗口的宽度和高度
import tkinter
win = tkinter.Tk()
win.geometry("100x100")
win.update()
print("当前窗口的宽度为",win.winfo_width())
print("当前窗口的高度为",win.winfo_height())
win.mainloop()
窗口居中
#-*- coding:utf-8 -*-
from tkinter import *
win = Tk()
sd = win.winfo_screenwidth() #得到屏幕宽度
sh = win.winfo_screenheight() #得到屏幕高度
wd = 450
wh = 300
x = (sd-wd) / 2 #居中
y = (sh-wh) / 2 #居中
win.geometry("%dx%d+%d+%d" %(wd,wh,x,y))
mainloop()
一、常用参数
| 语法 | 作用 |
|---|---|
| window= tk.TK() | 创建窗口 |
| window['height'] = 300 | 设置高 |
| window['width'] = 500 | 设置宽 |
| window.title('魔方小站') | 设置标题 |
| window['bg'] = '#0099ff' | 设置背景色 |
| window.geometry("500x300+120+100") | 设置窗口大小,+120指窗口距离左屏幕的距离 |
| window.option_add('*Font', 'Fira 10') | 设置全局字体 |
| window.resizable(width=False,height=True) | root.resizable(0,1) | 禁止窗口调整大小 |
| window.minsize(300,600) | 窗口可调整的最小值 |
| window.maxsize(600,1200) | 窗口可调整的最大值 |
| window.attributes("-toolwindow", 1) | 工具栏样式 |
| window.attributes("-topmost", -1) | 置顶窗口 |
| window.state("zoomed") | 窗口最大化 |
| window.iconify() | 窗口最小化 |
| window.deiconify() | 还原窗口 |
| window.attributes("-alpha",1) | 窗口透明化,透明度从 0-1,1 是不透明,0 是全透明 |
| window.destroy() | 关闭窗口 |
| window.iconbitmap("./image/icon.ico") | 设置窗口图标 |
| screenWidth = window.winfo_screenwidth() screenHeight = window.winfo_screenheight() |
获取屏幕宽高 |
| window.protocol("WM_DELETE_WINDOW", call) | 当窗口关闭时,执行call函数 |
| window.mainloop() | 主窗口循环更新 |
窗口attributes参数说明:
| 参数 | 作用 |
|---|---|
| alpha | 1.(Windows,Mac)控制窗口的透明度 2. 1.0 表示不透明,0.0 表示完全透明 3. 该选项并不支持所有的系统,对于不支持的系统,Tkinter 绘制一个不透明(1.0)的窗口 |
| disabled | (Windows)禁用整个窗口(这时候你只能从任务管理器中关闭它) |
| fullscreen | (Windows,Mac)如果设置为 True,则全屏显示窗口 |
| modified | (Mac)如果设置为 True,该窗口被标记为改动过 |
| titlepath | (Mac)设置窗口代理图标的路径 |
| toolwindow | (Windows)如果设置为 True,该窗口采用工具窗口的样式 |
| topmost | (Windows,Mac)如果设置为 True,该窗口将永远置于顶层 |
二、代码示例
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import tkinter as tk
# 创建窗体window = tk.Tk()
def call():
global window
window.destroy()
def main():
global window
# 设置主窗体大小
winWidth = 600
winHeight = 400
# 获取屏幕分辨率
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()
# 计算主窗口在屏幕上的坐标
x = int((screenWidth - winWidth)/ 2)
y = int((screenHeight - winHeight) / 2)
# 设置主窗口标题
window.title("主窗体参数说明")
# 设置主窗口大小
window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
# 设置窗口宽高固定
window.resizable(0,0)
# 设置窗口图标
window.iconbitmap("./image/icon.ico")
# 设置窗口顶部样式
window.attributes("-toolwindow", 0)
# 设置窗口透明度
window.attributes("-alpha",1)
#获取当前窗口状态
print(window.state())
window.protocol("WM_DELETE_WINDOW", call)
#循环更新
window.mainloop()
if __name__ == "__main__":
main()
|
https://www.cnblogs.com/yang-2018/p/11781535.html
https://stackoverflow.com/questions/4065783/how-do-i-get-a-windows-current-size-using-tkinter