Skip to content

Instantly share code, notes, and snippets.

@Zren
Last active Apr 24, 2021
Embed
What would you like to do?
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, Qt
from Xlib import X, Xatom
from Xlib.display import Display
from Xlib.error import CatchError, BadAtom
def enable_blur(widget: QtWidgets.QWidget):
ec = CatchError(BadAtom)
display = Display()
print('ec', ec)
print('display', display)
blurAtomId = display.intern_atom('_KDE_NET_WM_BLUR_BEHIND_REGION', only_if_exists=True)
print('blurAtomId', blurAtomId)
if blurAtomId == X.NONE:
print('_KDE_NET_WM_BLUR_BEHIND_REGION is not currently supported')
return
windowHandle = widget.windowHandle()
print('windowHandle', windowHandle)
winId = windowHandle.winId() # sip.voidptr
winId = winId.__int__() # https://stackoverflow.com/questions/47689778/how-do-you-go-from-a-sip-voidptr-qimage-constbits-to-a-ctypes-void-or-char-p
print('winId', winId)
window = display.create_resource_object('window', winId)
print('window', window, window.get_wm_class())
# Window.change_property(property, type, format, data, mode = X.PropModeReplace, onerror = None)
window.change_property(
blurAtomId,
Xatom.CARDINAL, # https://github.com/python-xlib/python-xlib/blob/master/Xlib/Xatom.py
32, # uint32
[0] # Treated as [0, 0, widget.width(), widget.height()] even after resizing window
)
atomName = display.get_atom_name(blurAtomId)
atomValue = window.get_property(blurAtomId, Xatom.CARDINAL, 0, 1)
print(atomName, atomValue)
def center_widget(w: QtWidgets.QWidget):
qt_rectangle = w.frameGeometry()
center_point = QtWidgets.QDesktopWidget().availableGeometry().center()
qt_rectangle.moveCenter(center_point)
w.move(qt_rectangle.topLeft())
class MainWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 400, 400)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.setWindowTitle("PyQt")
def updateBlur(self):
enable_blur(self)
def window():
app = QtWidgets.QApplication(sys.argv)
w = MainWidget()
center_widget(w)
bgRect = QtWidgets.QWidget(w)
bgRect.setStyleSheet('background-color: rgba(127,0,0, 0.5)')
bgRect.setGeometry(0, 0, w.width()/2, w.height())
label = QtWidgets.QLabel(w)
label.setText("Test Window")
label.setStyleSheet('color: yellow')
label.move(50, 20)
w.show()
w.updateBlur()
sys.exit(app.exec_())
if __name__ == '__main__':
window()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment