2019年9月29日 / 5,619次阅读 / Last Modified 2019年9月29日
tkinter
默认情况下,tkinter(Python标准GUI模块)的窗体都不是透明的,但是我们可以通过一个参数来控制其是否透明,以及透明的程度。
class winlog():
"""readonly modaless Toplevel log window class"""
def __init__(self, root=None, title='Log Window', **kw):
self.win = Toplevel(root)
self.win.title(title)
self.st = tklog(master=self.win, **kw)
self.st.pack(fill='both', expand=True)
self.win.bind('', self._focusIn)
self.win.bind('', self._focusOut)
def _focusIn(self, event):
self.win.attributes('-alpha', 1.0)
def _focusOut(self, evnet):
self.win.attributes('-alpha', 0.6)
def log(self, content, end='\n'):
self.st.log(content, end)
def warning(self, content, end='\n'):
self.st.warning(content, end)
def error(self, content, end='\n'):
self.st.error(content, end)
def destroy(self):
self.win.destroy()
这段代码来自我的一个基于tkinter的日志控件tklog项目,winlog窗体在获得焦点时,alpha参数设为1,在失去焦点时,alpha参数设为0.6。这个alpha参数,就是用来控制窗体透明度的,alpha值越小就,透明度就越高。
下图就是将alpha设置为0.6时,在Ubuntu桌面中的透明效果:
效果看着还不错,其实用Python的tkinter最GUI,还是很好玩的。而且,Python对tkinter的支持是最成熟的。
-- EOF --
本文链接:https://www.pynote.net/archives/1234
©Copyright 麦新杰 Since 2019 Python笔记