python的tkinter库关于创建窗口的一些属性设置

前言

tkinter库是python自带的功能库,功能强大,设计界面也简单,可能的不足就是界面不是很美观,然后代码量很大。但是自己学习和使用已经足够了,而且如果使用Canvas画布的功能,界面也可以做的非常漂亮。
这篇文章主要介绍一些关于窗口的参数设置,比如窗口的背景色,如何获取窗口的位置和大小,如何设置窗口透明,如何设置窗口为工具窗口,如何设置窗口为最顶层,如何去除窗口边框,如何鼠标拖动窗口。

窗口背景色

import tkinter as tk
top=tk.Tk()
top.title('我是一个窗口')
top['bg']='pink'	# 修改窗口背景色
top.mainloop()

在这里插入图片描述

获取窗口的位置和大小

需要使用到bind窗口函数

import tkinter as tk
def WinUpdate(event):
	x = top.winfo_x()
	y = top.winfo_y()
	top_w = top.winfo_width()
	top_h = top.winfo_height()
	print(f"窗口位于x:{x}-y:{y},窗口宽为{top_w}-高为{top_h}")
top=tk.Tk()
top.title('我是一个窗口')
top['bg']='pink'	# 修改窗口背景色
top.bind("<Configure>",WinUpdate)
top.mainloop()

在这里插入图片描述

设置窗口透明

需要设置透明色,然后将窗口颜色修改为此透明色,结果就是在窗口区域内,一切与设置的透明色相同的颜色都将变为透明。

import tkinter as tk
def WinUpdate(event):
	x = top.winfo_x()
	y = top.winfo_y()
	top_w = top.winfo_width()
	top_h = top.winfo_height()
	print(f"窗口位于x:{x}-y:{y},窗口宽为{top_w}-高为{top_h}")
top=tk.Tk()
top.title('我是一个窗口')

transparent_color = 'pink'

top['bg']=transparent_color	# 修改窗口背景色

top.wm_attributes("-transparentcolor", transparent_color)  # 设置窗口中<transparent_color>颜色部分设为透明,该部分相当于不再是窗口范围内,无法监测鼠标事件

top.bind("<Configure>",WinUpdate)
top.mainloop()

在这里插入图片描述

将窗口设置为工具窗口

工具窗口没有全屏和缩小化,只有关闭

import tkinter as tk
def WinUpdate(event):
	x = top.winfo_x()
	y = top.winfo_y()
	top_w = top.winfo_width()
	top_h = top.winfo_height()
	print(f"窗口位于x:{x}-y:{y},窗口宽为{top_w}-高为{top_h}")
top=tk.Tk()
top.title('我是一个窗口')

transparent_color = 'pink'

top['bg']=transparent_color	# 修改窗口背景色

# top.wm_attributes("-transparentcolor", transparent_color)  # 设置窗口中<transparent_color>颜色部分设为透明,该部分相当于不再是窗口范围内,无法监测鼠标事件

top.wm_attributes("-toolwindow",True)#设置为工具窗口,只能关闭,不能最小化

# top.bind("<Configure>",WinUpdate)
top.mainloop()

在这里插入图片描述

设置窗口位于屏幕最上方

即显示在屏幕的最上方。
加入下面一行代码:

top.wm_attributes("-topmost", True)  # 设置为最上面的窗口

在这里插入图片描述

去除窗口边框

这步需要谨慎操作,因为边框隐藏之后,右上角的关闭按钮也消失了,需要设置关闭按钮,来关闭窗口。

top.overrideredirect(True)#除去窗口的边框

在这里插入图片描述

窗口居中

win_width = top.winfo_screenwidth()  # 电脑屏幕宽度
win_height = top.winfo_screenheight()  # 电脑屏幕高度
top_w = int(1 / 3 * win_height)  # 窗口宽度	自己设定想要的
top_h = int(1 / 3 * win_height)  # 窗口高度
top_place = [int((win_width - top_w) / 2), int((win_height - top_h) / 3)]  # 窗口起始坐标
# print("窗口大小:",top_w,top_h)
top.geometry(str(top_w) + 'x' + str(top_h) + '+' + str(top_place[0]) + '+' + str(top_place[1]))  # 位置居中

在这里插入图片描述

鼠标拖动窗口

需要bind两个函数,一个是鼠标点击函数,一个鼠标拖动函数,然后实时的获取鼠标移动距离,去移动窗口位置。

import tkinter as tk

def WinUpdate(event):
	x = top.winfo_x()
	y = top.winfo_y()
	top_w = top.winfo_width()
	top_h = top.winfo_height()
	print(f"窗口位于x:{x}-y:{y},窗口宽为{top_w}-高为{top_h}")

def func1(event):
    new_x=top.winfo_x()+event.x-site_list[0]
    new_y=top.winfo_y()+event.y-site_list[1]
    if new_x>win_w-top_w:
        new_x=win_w-top_w
    if new_x<0:
        new_x=0
    if new_y<0:
        new_y=0
    if new_y>win_h-top_h:
        new_y=win_h-top_h
    site="{}x{}+{}+{}".format(top_w,top_h,new_x,new_y)
    top.geometry(site)

def func2(event):#记录移动前鼠标位置
    site_list[0]=event.x
    site_list[1]=event.y

def exit_top():
	top.destroy()	# 销毁窗口

site_list = [0,0]	# 保存鼠标的位置
top=tk.Tk()
top.title('我是一个窗口')

transparent_color = 'pink'

top['bg']=transparent_color	# 修改窗口背景色

# top.wm_attributes("-transparentcolor", transparent_color)  # 设置窗口中<transparent_color>颜色部分设为透明,该部分相当于不再是窗口范围内,无法监测鼠标事件

# top.wm_attributes("-toolwindow",True)#设置为工具窗口,只能关闭,不能最小化

# top.wm_attributes("-topmost", True)  # 设置为最上面的窗口

# top.overrideredirect(True)#除去窗口的边框

win_w = top.winfo_screenwidth()  # 电脑屏幕宽度
win_h = top.winfo_screenheight()  # 电脑屏幕高度
top_w = int(1 / 3 * win_h)  # 窗口宽度	自己设定想要的
top_h = int(1 / 3 * win_h)  # 窗口高度
top_place = [int((win_w - top_w) / 2), int((win_h - top_h) / 3)]  # 窗口起始坐标
# print("窗口大小:",top_w,top_h)
top.geometry(str(top_w) + 'x' + str(top_h) + '+' + str(top_place[0]) + '+' + str(top_place[1]))  # 位置居中


btn = tk.Button(top,text='exit',bd=0,bg='black',fg='red',command=exit_top)
btn.pack()

top.bind("<B1-Motion>",func1)#鼠标拖动事件
top.bind("<Button-1>",func2)#鼠标单击事件
# top.bind("<Configure>",WinUpdate)

top.mainloop()

不会制作动图,这里就没放置图片了。

最后

先这样吧。

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜小昊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值