是否可以将框架背景或任何其他小部件的颜色更改为透明浅蓝色或任何其他透明色?
答案 0 :(得分:7)
是的,有办法。不幸的是,它只适用于整个窗口(窗口和所有子窗口小部件)。
这是我前一段时间写过的一个小小的演示,它可以满足您的需求。
透明窗口演示:
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__()
答案 1 :(得分:0)
对于它的价值,这里是@rectangletangle的answer中的一个更具可读性的代码版本,其格式已重新设置为更严格地遵循PEP 8 - Style Guide for Python Code的建议。
除了对Tkinter模块的import
方式进行了小规模修改,使其可以在Python 2和3中都可以使用外,所有其他可执行代码都与他相同。
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
import re
class TransparentWin(tk.Tk):
""" Transparent Tkinter 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__()