Tkinter标签透明度 [英] Tkinter label transparency

查看:194
本文介绍了Tkinter标签透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

import tkinter as tk

myWin = tk.Tk()

myLabel = tk.Label(myWin, text="Hello World", bg="grey")
myLabel.pack()
myWin.wm_attributes("-transparentcolor","grey")

myWin.mainloop()

这使标签透明.

我想一种方法来控制标签的不透明性,而不是完全改变颜色,并保持标签可选,而不是选择tkinter win后面的窗口.

I would like a way to control the opacity of the label rather than completely change the color, as well as keep the label selectable rather than selecting the window behind the tkinter win.

预先感谢

推荐答案

您可以尝试创建自定义窗口小部件,例如这样的

You can try creating a custom widget, something like this for example

from tkinter import *

class _Toplevels():
    def __init__(self):
        self.toplevels=[]

    def append(self,toplevel):
        self.toplevels.append(toplevel)

    def lift(self):
        for top in self.toplevels:
            top.toplevel.lift()

    def position(self,event):
        try:
            for top in self.toplevels:
                top.position()
        except:
            pass

class TransparentLabel():
    def __init__(self,master,opacity=1,transcolor='SystemButtonFace',**kwargs):
        self.master=master
        self.opacity=opacity
        self.transcolor=transcolor
        self.toplevel=Toplevel(self.master)
        self.toplevel.configure(bg=self.transcolor)
        self.toplevel.overrideredirect(True)
        self.toplevel.attributes('-alpha',self.opacity)
        self.toplevel.wm_attributes("-transparentcolor",self.transcolor)
        self.toplevel.withdraw()
        self.label=Label(self.toplevel,**kwargs,bg=self.transcolor)
        toplevels.append(self)
        self.master.bind('<Configure>',toplevels.position)
        self.master.bind('<Map>',self._on_map)
        self.master.bind('<Unmap>',self._on_unmap)

    def _on_unmap(self,event):
        self.toplevel.withdraw()

    def _on_map(self,event):
        self.toplevel.deiconify()
        toplevels.position(None)

    def position(self):
        self.x=self.cover_frame.winfo_rootx()
        self.y=self.cover_frame.winfo_rooty()
        self.center_x=self.cover_frame.winfo_width()//2-self.dimentions[0]//2
        self.center_y=self.cover_frame.winfo_height()//2-self.dimentions[1]//2
        self.toplevel.geometry(f'+{self.center_x+self.x}+{self.center_y+self.y}')
        self.master.update_idletasks()
        toplevels.lift()

    def pack(self,**kwargs):
        self.cover_frame=Frame(self.master)
        self.label.pack(**kwargs)
        self.toplevel.update()
        self.dimentions=(self.toplevel.winfo_width(),self.toplevel.winfo_height())
        if 'padx' in kwargs:
            kwargs['padx']+=self.dimentions[0]//2
        else:
            kwargs['padx']=self.dimentions[0]//2
        if 'pady' in kwargs:
            kwargs['pady']+=self.dimentions[1]//2
        else:
            kwargs['pady']=self.dimentions[1]//2
        self.cover_frame.pack(**kwargs)
        self.cover_frame.pack_propagate(False)
        self.toplevel.deiconify()
        toplevels.position(None)

toplevels=_Toplevels()

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

label=TransparentLabel(root,text='Transparent Label 1',opacity=0.5)
label.pack(pady=20)
def_label=Label(text='Default Label')
def_label.pack()
label=TransparentLabel(root,text='Transparent Label 2')
label.pack(pady=20)

canvas=Canvas(bg='yellow')
canvas.pack()
nested_label=TransparentLabel(canvas,text='Transparent Label in Canvas')
nested_label.pack()

root.mainloop()

我基本上已经使用Toplevel来包含标签,因为我们现在可以直接在标签上修改各种属性.

I have basically used a Toplevel to contain the label, since we can now modify various attributes directly on it.

上面的示例将为您提供一个具有透明背景的小部件,您可以整体控制该小部件的不透明度.您还可以通过使用2个Toplevel s层,以类似于分别控制背景和前景不透明度的方式扩展此范围.

The above example will give you a widget that has transparent background and you can control the opacity of the widget as a whole. You could also extend this in a similar way to having individual control over the background and foreground opacity, using 2 layers of Toplevels.

更新

  • Resolved issue pointed out by @JacksonPro.

有趣的概念,但对于您的放置,我可能会注意到一些问题1)最大化然后最小化标签的位置变化2)当其放置在画布上时,标签的位置在窗口之外时再次最大化和最小化.

Interesting concept but I could notice few problems with regard to your placing 1) when maximized and then minimized the position of the label changes 2) when its placed over the canvas the position of the label is out of the window when the window is maximized and minimized again.

  • 改进的总体代码.
  • 这篇关于Tkinter标签透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆