GGG*_*GGG 9 qt pyqt pyqt5 pyqt6
我正在尝试在整个屏幕上绘制具有透明背景的图像。我对 Xlib 做了类似的事情,但我想我应该尝试 Qt,这样我的程序就可以在 X11 之外的不同环境中运行。在做出承诺之前,老实说,我只是在互联网上搜索试图找到一个可行的示例,但失败了。
所以我有一个想要覆盖在屏幕上的坐标图像。该图像是具有透明背景的PNG。这是我想要看到的图片,带有坐标的桌面:

这是我的 PyQt6 代码,它全屏绘制图像,但遗憾的是你看不到它,它有黑色背景:
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QImage
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.QtGui import QPainter
class Img(QMainWindow):
def __init__(self, img_path, parent=None):
super().__init__(parent)
self.qimg = QImage(img_path)
self.setStyleSheet('QMainWindow {background:transparent}')
self.setWindowFlags(
Qt.WindowType.WindowStaysOnTopHint |
Qt.WindowType.FramelessWindowHint |
Qt.WindowType.WindowTransparentForInput
)
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
def paintEvent(self, qpaint_event):
painter = QPainter(self)
rect = qpaint_event.rect()
painter.drawImage(rect, self.qimg)
self.showFullScreen()
if __name__ == "__main__":
app = QApplication(sys.argv)
img_path = 'coords.png'
window = Img(img_path)
window.show()
sys.exit(app.exec())
Run Code Online (Sandbox Code Playgroud)
答案不需要是 PyQt6,也许是 PyQt5 或用 Go 编写。
我已经尝试过你的代码,它工作正常,显然这似乎是图像的问题,我建议你使用某种外部应用程序来擦除这样的透明部分。
如果你想自动执行此操作,我建议你使用 opencv。
| 归档时间: |
|
| 查看次数: |
1461 次 |
| 最近记录: |