使用 QPainter 和 paintEvent 在 PYQT5 中的 QLabel 中包含的 Pixmap 上绘制圆圈
Using QPainter and paintEvent to draw circles on a Pixmap that is contained in a QLabel in PYQT5
我是 PYQT5 的新手,我想在 PYQT5 的 MainWindow UI 的 QLabel 中包含的 Pixmap 上使用以下代码绘制一个圆圈:
from PyQt5 import QtCore, QtGui, QtWidgets
background_image_path = '001_01_01_041_05.png'
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 60, 331, 401))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self._image = QtGui.QPixmap(background_image_path)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setPixmap(self._image)
def paintEvent(self, event):
painter = QtGui.QPainter(self._image)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(5)
painter.setPen(pen)
painter.drawEllipse(300, 300, 70, 70)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
但是,我无法调用 paintEvent(self, event) 函数。当我确实开始调用 paintEvent(self, event) 时,我看不到在像素图上绘制的圆圈。我尝试将 QPainter 设置为 Pixmap 本身(在 painter = QtGui.QPainter(self._image) 中),但这也不起作用。
我做错了什么?
下面的代码有效,这是我的代码所基于的:
from PyQt5 import QtWidgets, QtGui, QtCore
background_image_path = '001_01_01_041_05.png'
class ImageScroller(QtWidgets.QWidget):
def __init__(self):
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(5)
painter.setPen(pen)
painter.drawEllipse(300, 300, 70, 70)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = ImageScroller()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
如有任何想法或建议,我们将不胜感激!
你漏了几行。
试一试:
from PyQt5 import QtWidgets, QtGui, QtCore
background_image_path = 'E:/_Qt/img/heart.png' # '001_01_01_041_05.png'
class ImageScroller(QtWidgets.QWidget):
def __init__(self):
super().__init__() # <-------
self._image = QtGui.QPixmap(background_image_path) # <-------
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(5)
painter.setPen(pen)
painter.drawEllipse(300, 300, 70, 70)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = ImageScroller()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
真正的问题是你在编辑pyuic生成的模块,
这总是一个非常糟糕的主意。试一试:
from PyQt5 import QtCore, QtGui, QtWidgets
background_image_path = 'E:/_Qt/img/heart.png'
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 0, 760, 560))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# self._image = QtGui.QPixmap(background_image_path)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
# self.label.setPixmap(self._image)
self.label.setText(_translate("MainWindow",
"""
It is not recommended to modify the design file,
it is appropriate to create another file
to join the logic with the design.
"""))
# def paintEvent(self, event):
# painter = QtGui.QPainter(self._image)
# painter.drawPixmap(self.rect(), self._image)
# pen = QtGui.QPen()
# pen.setWidth(5)
# painter.setPen(pen)
# painter.drawEllipse(300, 300, 70, 70)
class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyApp, self).__init__()
self.setupUi(self)
self._image = QtGui.QPixmap(background_image_path)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
#pen = QtGui.QPen()
#pen.setWidth(5)
#painter.setPen(pen)
painter.setPen(QtGui.QPen(QtCore.Qt.blue, 5))
painter.drawEllipse(350, 350, 70, 70)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# MainWindow = QtWidgets.QMainWindow()
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
# MainWindow.show()
window = MyApp()
window.show()
sys.exit(app.exec_())
我是 PYQT5 的新手,我想在 PYQT5 的 MainWindow UI 的 QLabel 中包含的 Pixmap 上使用以下代码绘制一个圆圈:
from PyQt5 import QtCore, QtGui, QtWidgets
background_image_path = '001_01_01_041_05.png'
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 60, 331, 401))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self._image = QtGui.QPixmap(background_image_path)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setPixmap(self._image)
def paintEvent(self, event):
painter = QtGui.QPainter(self._image)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(5)
painter.setPen(pen)
painter.drawEllipse(300, 300, 70, 70)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
但是,我无法调用 paintEvent(self, event) 函数。当我确实开始调用 paintEvent(self, event) 时,我看不到在像素图上绘制的圆圈。我尝试将 QPainter 设置为 Pixmap 本身(在 painter = QtGui.QPainter(self._image) 中),但这也不起作用。
我做错了什么?
下面的代码有效,这是我的代码所基于的:
from PyQt5 import QtWidgets, QtGui, QtCore
background_image_path = '001_01_01_041_05.png'
class ImageScroller(QtWidgets.QWidget):
def __init__(self):
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(5)
painter.setPen(pen)
painter.drawEllipse(300, 300, 70, 70)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = ImageScroller()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
如有任何想法或建议,我们将不胜感激!
你漏了几行。 试一试:
from PyQt5 import QtWidgets, QtGui, QtCore
background_image_path = 'E:/_Qt/img/heart.png' # '001_01_01_041_05.png'
class ImageScroller(QtWidgets.QWidget):
def __init__(self):
super().__init__() # <-------
self._image = QtGui.QPixmap(background_image_path) # <-------
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(5)
painter.setPen(pen)
painter.drawEllipse(300, 300, 70, 70)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = ImageScroller()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
真正的问题是你在编辑pyuic生成的模块, 这总是一个非常糟糕的主意。试一试:
from PyQt5 import QtCore, QtGui, QtWidgets
background_image_path = 'E:/_Qt/img/heart.png'
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 0, 760, 560))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# self._image = QtGui.QPixmap(background_image_path)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
# self.label.setPixmap(self._image)
self.label.setText(_translate("MainWindow",
"""
It is not recommended to modify the design file,
it is appropriate to create another file
to join the logic with the design.
"""))
# def paintEvent(self, event):
# painter = QtGui.QPainter(self._image)
# painter.drawPixmap(self.rect(), self._image)
# pen = QtGui.QPen()
# pen.setWidth(5)
# painter.setPen(pen)
# painter.drawEllipse(300, 300, 70, 70)
class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyApp, self).__init__()
self.setupUi(self)
self._image = QtGui.QPixmap(background_image_path)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
#pen = QtGui.QPen()
#pen.setWidth(5)
#painter.setPen(pen)
painter.setPen(QtGui.QPen(QtCore.Qt.blue, 5))
painter.drawEllipse(350, 350, 70, 70)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# MainWindow = QtWidgets.QMainWindow()
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
# MainWindow.show()
window = MyApp()
window.show()
sys.exit(app.exec_())