当前位置:   article > 正文

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

tkinter去除标题栏
源码+依赖素材

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

效果

主要代码(网盘资源分享的源码更新在下面,懒得改了,自己下载以后复制粘贴覆盖一下)
  1. # 作者:昌龙XL
  2. import tkinter as tk
  3. from PIL import Image, ImageTk
  4. from ctypes import windll
  5. import time
  6. # 微秒级等待
  7. def micro_sleep(sec):
  8. st = time.perf_counter()
  9. while time.perf_counter() - st < sec:
  10. pass
  11. # 窗口渐显/隐
  12. def window_gradually_(mode='displays'):
  13. if mode == 'displays':
  14. for i in range(20):
  15. root.attributes("-alpha",(i+1)/20)
  16. root.update()
  17. micro_sleep(0.003)
  18. root.attributes("-alpha",1)
  19. elif mode == 'hide':
  20. for i in range(20):
  21. root.attributes("-alpha",1-((i+1)/20))
  22. root.update()
  23. micro_sleep(0.003)
  24. root.attributes("-alpha",0)
  25. root = tk.Tk()
  26. # DPI调节,通俗点就是变清晰
  27. windll.shcore.SetProcessDpiAwareness(1)
  28. ScaleFactor = windll.shcore.GetScaleFactorForDevice(0)
  29. root.tk.call('tk', 'scaling', ScaleFactor/75)
  30. glass_color = 'grey15' # 可以改颜色,16进制或颜色代码(如red)
  31. root.config(bg=glass_color)
  32. root.geometry(f'1000x380+{root.winfo_screenwidth() // 2 - 300}+{root.winfo_screenheight() // 2 - 300}')
  33. root.attributes('-transparentcolor', glass_color)
  34. root.overrideredirect(True)
  35. # 背景
  36. WBGImg = Image.open('./Pic/WindowBG_white.png')
  37. WBG = tk.Canvas(
  38. root, bg=glass_color, width=WBGImg.width, height=WBGImg.height, highlightthickness=0
  39. )
  40. WBG.pack()
  41. WBGImg = ImageTk.PhotoImage(WBGImg)
  42. WBG.create_image(0,0,image=WBGImg,anchor='nw')
  43. # 窗口移动
  44. def MouseDown(event):
  45. global mousX
  46. global mousY
  47. mousX=event.x
  48. mousY=event.y
  49. def MouseMove(event):
  50. root.geometry(f'+{event.x_root - mousX}+{event.y_root - mousY}')
  51. root.bind('<Button-1>',MouseDown)
  52. root.bind('<B1-Motion>',MouseMove)
  53. # 添加到任务栏
  54. GWL_EXSTYLE = -20
  55. WS_EX_APPWINDOW = 0x00040000
  56. WS_EX_TOOLWINDOW = 0x00000080
  57. def set_appwindow(root):
  58. hwnd = windll.user32.GetParent(root.winfo_id())
  59. style = windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
  60. style = style & ~WS_EX_TOOLWINDOW
  61. style = style | WS_EX_APPWINDOW
  62. windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, style)
  63. root.wm_withdraw()
  64. root.after(10, lambda: root.wm_deiconify())
  65. root.after(10, lambda: set_appwindow(root=root))
  66. # 关闭
  67. def ClosePress(event=None):
  68. WBG.create_image(999,1,image=closeB_p,anchor='ne',tag='closeB_P')
  69. def Winquit(event=None):
  70. window_gradually_(mode='hide')
  71. WBG.delete('closeB_P')
  72. root.destroy()
  73. closeB_n = ImageTk.PhotoImage(Image.open('./Pic/CloseButton_normal.png'))
  74. closeB_p = ImageTk.PhotoImage(Image.open('./Pic/CloseButton_press.png'))
  75. WBG.create_image(999,1,image=closeB_n,anchor='ne',tag='closeB_N')
  76. WBG.tag_bind('closeB_N','<Button-1>',ClosePress)
  77. WBG.tag_bind('closeB_N','<ButtonRelease-1>',Winquit)
  78. # 最小化
  79. def MiniPress(event=None):
  80. WBG.create_image(929,1,image=miniB_p,anchor='ne',tag='miniB_P')
  81. c = 0
  82. smroot = False
  83. def Winminimize(event=None):
  84. global smroot
  85. WBG.delete('miniB_P')
  86. WBG.tag_unbind('miniB_N','<Button-1>')
  87. WBG.tag_unbind('miniB_N','<ButtonRelease-1>')
  88. linshi_window = tk.Toplevel(root)
  89. linshi_window.attributes("-alpha",0)
  90. linshi_window.update()
  91. window_gradually_(mode='hide')
  92. root.overrideredirect(False)
  93. root.iconify()
  94. root.bind('<Map>',reWinminimize)
  95. linshi_window.destroy()
  96. del linshi_window
  97. smroot = True
  98. WBG.tag_bind('miniB_N','<Button-1>',MiniPress)
  99. WBG.tag_bind('miniB_N','<ButtonRelease-1>',Winminimize)
  100. micro_sleep(0.05)
  101. def reWinminimize(event=None):
  102. global c
  103. c += 1
  104. if c == 2:
  105. linshi_window = tk.Toplevel(root)
  106. linshi_window.attributes("-alpha",0)
  107. root.unbind('<Map>')
  108. root.deiconify()
  109. linshi_window.update()
  110. root.overrideredirect(True)
  111. c = 0
  112. root.after(10, lambda: set_appwindow(root=root))
  113. window_gradually_()
  114. linshi_window.destroy()
  115. del linshi_window
  116. micro_sleep(0.05)
  117. miniB_n = ImageTk.PhotoImage(Image.open('./Pic/MinimizeButton_normal.png'))
  118. miniB_p = ImageTk.PhotoImage(Image.open('./Pic/MinimizeButton_press.png'))
  119. WBG.create_image(929,1,image=miniB_n,anchor='ne',tag='miniB_N')
  120. WBG.tag_bind('miniB_N','<Button-1>',MiniPress)
  121. WBG.tag_bind('miniB_N','<ButtonRelease-1>',Winminimize)
  122. # 渐显窗口
  123. def show_window():
  124. window_gradually_()
  125. root.after(50,show_window_callback)
  126. root.mainloop()
原理

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

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

坚持无废话创作!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/718695
推荐阅读
相关标签
  

闽ICP备14008679号