python - 透明颜色 Tkinter

标签 python tkinter

是否可以将框架背景或任何其他小部件的颜色更改为透明浅蓝色或任何其他透明颜色?

最佳答案

是的,有办法。不幸的是,它仅适用于整个窗口(窗口和所有子窗口小部件)。

这是我不久前写的一个小演示,除其他外,它可以满足您的需求。

透明窗口演示:

import Tkinter as Tk, re




class TransparentWin (Tk.Tk) :
    ''' Transparent Tk Window Class '''

    def __init__ (self) :

        Tk.Tk.__init__(self)

        self.Drag = Drag(self)

        ''' Sets focus to the window. '''
        self.focus_force()

        ''' Removes the native window boarder. '''
        self.overrideredirect(True)

        ''' Disables resizing of the widget.  '''
        self.resizable(False, False)

        ''' Places window above all other windows in the window stack. '''
        self.wm_attributes("-topmost", True)

        ''' This changes the alpha value (How transparent the window should
            be). It ranges from 0.0 (completely transparent) to 1.0
            (completely opaque).  '''
        self.attributes("-alpha", 0.7)

        ''' The windows overall position on the screen  '''
        self.wm_geometry('+' + str(439) + '+' + str(172))

        ''' Changes the window's color. '''
        bg = '#3e4134'

        self.config(bg=bg)

        self.Frame = Tk.Frame(self, bg=bg)
        self.Frame.pack()

        ''' Exits the application when the window is right clicked. '''
        self.Frame.bind('<Button-3>', self.exit)

        ''' Changes the window's size indirectly. '''
        self.Frame.configure(width=162, height=100)

    def exit (self, event) :
        self.destroy()

    def position (self) :

        _filter = re.compile(r"(\d+)?x?(\d+)?([+-])(\d+)([+-])(\d+)")

        pos = self.winfo_geometry()

        filtered = _filter.search(pos)
        self.X = int(filtered.group(4))
        self.Y = int(filtered.group(6))

        return self.X, self.Y

class Drag:
    ''' Makes a window dragable. '''

    def __init__ (self, par, dissable=None, releasecmd=None) :

        self.Par        = par
        self.Dissable   = dissable

        self.ReleaseCMD = releasecmd

        self.Par.bind('<Button-1>', self.relative_position)
        self.Par.bind('<ButtonRelease-1>', self.drag_unbind)


    def relative_position (self, event) :

        cx, cy = self.Par.winfo_pointerxy()
        x, y = self.Par.position()

        self.OriX = x
        self.OriY = y

        self.RelX = cx - x
        self.RelY = cy - y

        self.Par.bind('<Motion>', self.drag_wid)

    def drag_wid (self, event) :

        cx, cy = self.Par.winfo_pointerxy()

        d = self.Dissable

        if d == 'x' :
            x = self.OriX
            y = cy - self.RelY
        elif d == 'y' :
            x = cx - self.RelX
            y = self.OriY
        else:
            x = cx - self.RelX
            y = cy - self.RelY

        if x < 0 :
            x = 0

        if y < 0 :
            y = 0

        self.Par.wm_geometry('+' + str(x) + '+' + str(y))

    def drag_unbind (self, event) :

        self.Par.unbind('<Motion>')

        if self.ReleaseCMD != None :
            self.ReleaseCMD()


    def dissable (self) :

        self.Par.unbind('<Button-1>')
        self.Par.unbind('<ButtonRelease-1>')
        self.Par.unbind('<Motion>')


def __run__ () :

    TransparentWin().mainloop()


if __name__ == '__main__' :

    __run__()

关于python - 透明颜色 Tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6104991/

相关文章:

python - 像 PHP 一样用 Python 发送电子邮件

Python如何删除包含列表作为元素的数据框中不需要的逗号

Python - 子类打开?

python - 如何在 Tkinter TkTable 中插入/设置数据

python - 如何在Python中使用filedialog将DataFrame保存到Excel文件?

python - 创建日志包装器

python - 运行时警告 : divide by zero encountered in true_divide W = 1/sigma**2/s_sq symfit

python - 读取txt文件的内容并在Tkinter GUI Python中显示

python - 如何通过按键事件绑定(bind)更新 tkinter 类中的图像?

Python:PIL - [Errno 32] 保存 .png 时管道损坏