由于所有窗口管理器都不支持此功能,我认为在显示背景之前复制背景
但这会带来一些问题
- 背景并不总是正确备份。我不明白为什么。
- 使用此方法无法使用鼠标移动窗口。因为使用hide()和show()会失去焦点。
您认为这些问题有解决方案吗?
#!/bin/env python3
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QPainter, QPen
from PyQt5.QtWidgets import QApplication, QMainWindow
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
self.offset= None
self.ps= app.primaryScreen()
self.setGeometry(500, 500, 200, 200)
self.saveBackground()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
elif event.key() in [ Qt.Key_Left, Qt.Key_Right, Qt.Key_Up, Qt.Key_Down ]:
point= self.geometry().topLeft()
if event.key() == Qt.Key_Left:
point.setX(point.x() - 1)
elif event.key() == Qt.Key_Right:
point.setX(point.x() + 1)
elif event.key() == Qt.Key_Up:
point.setY(point.y() - 1)
elif event.key() == Qt.Key_Down:
point.setY(point.y() + 1)
self.move(point)
self.saveBackground()
def mousePressEvent(self, event):
self.offset= event.pos()
def mouseMoveEvent(self, event):
self.move(self.mapToParent(event.pos() - self.offset))
self.saveBackground()
def paintEvent(self, event):
qp= QPainter()
qp.begin(self)
qp.drawPixmap(0, 0, self.pix)
qp.setPen(QPen(QColor(255, 0, 0), 2, Qt.SolidLine))
qp.drawRect(1, 1, self.width() - 2, self.height() - 2)
def saveBackground(self):
geo= self.frameGeometry()
self.hide()
self.pix= self.ps.grabWindow(app.desktop().winId(), geo.x(), geo.y(), geo.width(), geo.height())
self.show()
if __name__ == "__main__":
app= QApplication([])
ui= Window()
ui.show()
exit(app.exec_())
答案 0 :(得分:0)
#!/bin/env python3
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QPainter, QPen
from PyQt5.QtWidgets import QApplication, QMainWindow
from time import time
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
self.offset= None
self.ps= app.primaryScreen()
self.setGeometry(500, 500, 200, 200)
self.saveBackground()
self.csec= time()
self.moving= False
self.ratio= 2
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
elif event.key() in [ Qt.Key_Left, Qt.Key_Right, Qt.Key_Up, Qt.Key_Down ]:
if time() > self.csec + 1.2:
point= self.geometry().topLeft()
if event.key() == Qt.Key_Left:
point.setX(point.x() - 1)
elif event.key() == Qt.Key_Right:
point.setX(point.x() + 1)
elif event.key() == Qt.Key_Up:
point.setY(point.y() - 1)
elif event.key() == Qt.Key_Down:
point.setY(point.y() + 1)
self.move(point)
self.csec= time()
self.saveBackground()
super().keyPressEvent(event)
def mousePressEvent(self, event):
self.offset= event.pos()
self.moving= True
self.repaint()
def mouseReleaseEvent(self, event):
self.saveBackground()
self.moving= False
self.repaint()
def mouseMoveEvent(self, event):
self.move(self.mapToParent(event.pos() - self.offset))
def paintEvent(self, event):
qp= QPainter()
qp.begin(self)
if self.moving:
qp.setBrush(QColor(220, 220, 220))
qp.drawRect(0, 0, self.width() - 1, self.height() - 1)
else:
qp.drawPixmap(0, 0, self.pix.scaled(self.size() * self.ratio, Qt.KeepAspectRatio))
qp.setPen(QPen(QColor(255, 0, 0), 1, Qt.SolidLine))
qp.drawRect(0, 0, self.width() - 1, self.height() - 1)
def saveBackground(self):
self.hide()
self.pix= self.ps.grabWindow(app.desktop().winId(), self.x(), self.y(), self.width(), self.height())
self.show()
if __name__ == "__main__":
app= QApplication([])
ui= Window()
ui.show()
exit(app.exec_())