【问题标题】:How to create a translucent window in tinter?如何在 tkinter 中创建半透明窗口?
【发布时间】:2021-09-17 05:02:33
【问题描述】:

我正在尝试在 Tkinter 中创建一个半透明窗口,就像 windows 11 中的那个一样

如何做到这一点?如果我们不能做到这一点,我们是否可以捕获屏幕的一部分并使用 cv2 对其进行模糊处理并将其用作不断更新的背景?

【问题讨论】:

  • 我认为这可能会有所帮助stackoverflow.com/questions/19080499/…
  • 该链接给出了使其透明的答案,我想让它像 Windows 11(半透明玻璃)中一样。关于如何做到这一点的任何想法?
  • @Sanjay tkinter 无需大量编码即可提供的最佳功能是使用root.attributes("-alpha", 0.5) 使窗口略微透明(不是半透明)
  • 嘿,我刚刚在下面添加了代码来使用tkinter,这在您制作启动画面时很有用,请看一下:D

标签: python user-interface tkinter window


【解决方案1】:

不,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 的输出:

【讨论】:

  • 不使用pygetwindow,为什么不使用root.winfo_rootx()root.winfo_rooty()root.winfo_width()root.winfo_height()
  • @TheLizzard 我最初的解决方案实际上涉及使用winfo_geometry() 并从那里获取尺寸,但我也尝试了不同的东西,包括winfo_height(),最后我只是复制粘贴了代码,但它会更好,我会编辑它
  • 你也可以使用 pypi 中的 BlurWindow 来使用原生窗口模糊
【解决方案2】:

对于实时模糊(本机 Windows 模糊),请使用 "BlurWindow"

python -m pip install BlurWindow

from tkinter import *
from ctypes import windll

from BlurWindow.blurWindow import blur

root = Tk()
root.config(bg='green')

root.wm_attributes("-transparent", 'green')
root.geometry('500x400')

root.update()

hWnd = windll.user32.GetForegroundWindow()
blur(hWnd)



def color(hex):
    hWnd = windll.user32.GetForegroundWindow()
    blur(hWnd,hexColor=hex)
    

e = Entry(width=9)
e.insert(0,'#12121240')

e.pack()
b = Button(text='Apply',command=lambda:[color(e.get())])
b.pack()


root.mainloop()

【讨论】:

    猜你喜欢
    • 2017-02-15
    • 2016-03-03
    • 1970-01-01
    • 2021-04-10
    • 2014-11-25
    • 2013-08-26
    • 2011-10-12
    • 2021-04-15
    相关资源
    最近更新 更多