前言
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()
不会制作动图,这里就没放置图片了。
最后
先这样吧。