Tkinter 之主窗口参数
一、常用参数
语法 | 作用 |
---|---|
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,该窗口将永远置于顶层 |
获得窗口的宽度和高度
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()
二、代码示例
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()
import tkinter as tk #导入模块 命名为 tk # 创建窗口 window = tk.Tk() # 调整窗口大小和位置,单位是像素 width, height = 500, 300 place_x, place_y = 100, 100 #位置以屏幕左上角为起始点(0,0) window.geometry(f'{width}x{height}+{place_x}+{place_y}') # 设置窗口标题 window.title('My window') # 设置窗口背景颜色 window.config(bg='black') # 窗口创建后一定时间执行程序 def config_() : #定义一个打印字符串 ‘Hello world’ 的函数 print("Hello world") window.config(bg='white') window.after(2000, config_) #第一个参数是时间,单位是毫秒;第二个参数是要执行的程序,注意,不用加括号 # 隐藏窗口外部 window.overrideredirect(True) # 固定窗口尺寸 window.resizable(0, 0) # 给窗口添加图标 #icon = tk.PhotoImage(file = '/pilogo.png') #window.iconphoto(True, icon) # 设置窗口最小|最大尺寸 window.minsize(150, 100) #最小尺寸 window.maxsize(500, 300) #最大尺寸 # 窗口创建4秒后关闭窗口 window.after(4000, window.destroy) # 窗口全屏 #window.attributes('-fullscreen', True) # 窗口置顶 window.attributes('-topmost', True) window.mainloop()
#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()
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.
REF
https://www.cnblogs.com/yang-2018/p/11781535.html
https://stackoverflow.com/questions/4065783/how-do-i-get-a-windows-current-size-using-tkinter
https://blog.csdn.net/weixin_39932300/article/details/111431678