python - 如何在 tinter 中创建半透明窗口?
问题描述
我正在尝试在 Tkinter 中创建一个半透明窗口,就像 Windows 11 中的那样
这个怎么做?如果我们不能做到这一点,我们是否可以捕获屏幕的一部分并使用 cv2 对其进行模糊处理并将其用作不断更新的背景?
解决方案
不,这不能直接用tkinter
. 但:
如果你使用PIL
,你可以获取窗口的位置,然后截图,然后模糊它,然后将它作为你的应用程序背景。但是,如果用户尝试移动/调整应用程序的大小,这将不起作用。但这里有一个粗略的代码:
from tkinter import *
from PIL import ImageTk, ImageGrab, ImageFilter # pip install Pillow
root = Tk()
root.overrideredirect(1) # Hide the titlebar etc..
bg = Canvas(root)
bg.pack(fill='both',expand=1)
root.update()
# Get required size and then add pixels to remove title bar and window shadow
left = root.winfo_rootx()
top = root.winfo_rooty()
right = left + root.winfo_width()
bottom = top + root.winfo_height()
root.withdraw() # Hide the window
img = ImageGrab.grab((left,top,right,bottom)) # Get the bg image
root.deiconify() # Show the window
img = img.filter(ImageFilter.GaussianBlur(radius=5)) # Blur it
img = ImageTk.PhotoImage(img)
bg.create_image(0,0, image=img, anchor='nw') # Show in canvas
label = Label(root,text='This is a translucent looking app')
bg.create_window(bg.winfo_width()/2,bg.winfo_height()/2,window=label) # Position in the center
root.mainloop()
输出tkinter
:
tkinter
如果您想追求现代外观,使用PyQt
和检查qtacrylic不是最佳选择
输出PyQt
:
推荐阅读
- android - 未收到通知,但在 Android 中使用 Firebase 和 Retrofit2 似乎发送成功
- reactjs - 动作正在更新状态,但来自 mapStateToProps 的道具在组件中显示为未定义
- firebase - 移至 FCM 旧版 HTTP API
- xml - 填充视口所需的最简单的 svg 代码
- javascript - KonvaJS - 如何使用按钮触发器添加形状?
- reactjs - React Native 与 Expo 监控来自麦克风的用户语音音高
- anaconda - 安装标记器后,我在我的 conda 环境中找不到未封装的 bert base
- xml - 我想返回一个我已经使用 Go 的 XML 响应
- android - Android:kotlin协程后台任务
- php - js树中如何排列前缀编号?