Python tkinter 去标题栏+圆角窗口+各种优化 の 基本窗口

源码+依赖素材

PyTk窗口.zip官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘

效果

主要代码(网盘资源分享的源码更新在下面,懒得改了,自己下载以后复制粘贴覆盖一下)
# 作者:昌龙XL
import tkinter as tk
from PIL import Image, ImageTk
from ctypes import windll
import time

# 微秒级等待
def micro_sleep(sec):
    st = time.perf_counter()
    while time.perf_counter() - st < sec:
        pass
    
# 窗口渐显/隐
def window_gradually_(mode='displays'):
    if mode == 'displays':
        for i in range(20):
            root.attributes("-alpha",(i+1)/20)
            root.update()
            micro_sleep(0.003)
        root.attributes("-alpha",1)
    elif mode == 'hide':
        for i in range(20):
            root.attributes("-alpha",1-((i+1)/20))
            root.update()
            micro_sleep(0.003)
        root.attributes("-alpha",0)

root = tk.Tk()
# DPI调节,通俗点就是变清晰
windll.shcore.SetProcessDpiAwareness(1)
ScaleFactor = windll.shcore.GetScaleFactorForDevice(0)
root.tk.call('tk', 'scaling', ScaleFactor/75)

glass_color = 'grey15'  # 可以改颜色,16进制或颜色代码(如red)
root.config(bg=glass_color)
root.geometry(f'1000x380+{root.winfo_screenwidth() // 2 - 300}+{root.winfo_screenheight() // 2 - 300}')
root.attributes('-transparentcolor', glass_color)
root.overrideredirect(True)

# 背景
WBGImg = Image.open('./Pic/WindowBG_white.png')
WBG = tk.Canvas(
    root, bg=glass_color, width=WBGImg.width, height=WBGImg.height, highlightthickness=0
)
WBG.pack()
WBGImg = ImageTk.PhotoImage(WBGImg)
WBG.create_image(0,0,image=WBGImg,anchor='nw')

# 窗口移动
def MouseDown(event):
    global mousX
    global mousY
    mousX=event.x
    mousY=event.y
def MouseMove(event):
    root.geometry(f'+{event.x_root - mousX}+{event.y_root - mousY}')
root.bind('<Button-1>',MouseDown)
root.bind('<B1-Motion>',MouseMove)

# 添加到任务栏
GWL_EXSTYLE = -20
WS_EX_APPWINDOW = 0x00040000
WS_EX_TOOLWINDOW = 0x00000080
def set_appwindow(root):
    hwnd = windll.user32.GetParent(root.winfo_id())
    style = windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
    style = style & ~WS_EX_TOOLWINDOW
    style = style | WS_EX_APPWINDOW
    windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, style)
    root.wm_withdraw()
    root.after(10, lambda: root.wm_deiconify())
root.after(10, lambda: set_appwindow(root=root))

# 关闭
def ClosePress(event=None):
    WBG.create_image(999,1,image=closeB_p,anchor='ne',tag='closeB_P')
def Winquit(event=None):
    window_gradually_(mode='hide')
    WBG.delete('closeB_P')
    root.destroy()
closeB_n = ImageTk.PhotoImage(Image.open('./Pic/CloseButton_normal.png'))
closeB_p = ImageTk.PhotoImage(Image.open('./Pic/CloseButton_press.png'))
WBG.create_image(999,1,image=closeB_n,anchor='ne',tag='closeB_N')
WBG.tag_bind('closeB_N','<Button-1>',ClosePress)
WBG.tag_bind('closeB_N','<ButtonRelease-1>',Winquit)

# 最小化
def MiniPress(event=None):
    WBG.create_image(929,1,image=miniB_p,anchor='ne',tag='miniB_P')
c = 0
smroot = False
def Winminimize(event=None):
    global smroot
    WBG.delete('miniB_P')
    WBG.tag_unbind('miniB_N','<Button-1>')
    WBG.tag_unbind('miniB_N','<ButtonRelease-1>')
    linshi_window = tk.Toplevel(root)
    linshi_window.attributes("-alpha",0)
    linshi_window.update()
    window_gradually_(mode='hide')
    root.overrideredirect(False)
    root.iconify()
    root.bind('<Map>',reWinminimize)
    linshi_window.destroy()
    del linshi_window
    smroot = True
    WBG.tag_bind('miniB_N','<Button-1>',MiniPress)
    WBG.tag_bind('miniB_N','<ButtonRelease-1>',Winminimize)
    micro_sleep(0.05)
def reWinminimize(event=None):
    global c
    c += 1
    if c == 2:
        linshi_window = tk.Toplevel(root)
        linshi_window.attributes("-alpha",0)
        root.unbind('<Map>')
        root.deiconify()
        linshi_window.update()
        root.overrideredirect(True)
        c = 0
        root.after(10, lambda: set_appwindow(root=root))
        window_gradually_()
        linshi_window.destroy()
        del linshi_window
    micro_sleep(0.05)
miniB_n = ImageTk.PhotoImage(Image.open('./Pic/MinimizeButton_normal.png'))
miniB_p = ImageTk.PhotoImage(Image.open('./Pic/MinimizeButton_press.png'))
WBG.create_image(929,1,image=miniB_n,anchor='ne',tag='miniB_N')
WBG.tag_bind('miniB_N','<Button-1>',MiniPress)
WBG.tag_bind('miniB_N','<ButtonRelease-1>',Winminimize)

# 渐显窗口
def show_window():
    window_gradually_()
root.after(50,show_window_callback)

root.mainloop()
原理

选定一个颜色为透明色(代码中为grey15),把背景和一Canvas控件设置成此颜色,在利用Canvas绘制提前设置好的圆角图片。

最小化等功能是作者自己好不容易研究出来的,因为过于复杂,就不解释了。

坚持无废话创作!!!

  • 9
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值