PyQt5 绘画和像素
你可以使用 QPainter 小控件在 PyQt5 窗口中绘制。与其他小控件不同,此小控件支持在小控件内添加像素(点)。在本文中,我们将解释如何在 Python 中使用 QPainter 小控件。
要在 Qt5 中使用小控件,我们导入 PyQt5.QtGui。这还包含其他类,如 QPen 和 QColor。
QPainter 小控件示例
我们使用以下方法设置窗口背景:
# Set window background color
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.white)
self.setPalette(p)
使用 drawPoint(x,y) 方法添加像素。

PyQt5 QPainter 示例
下面的示例描绘了 QPainter 小控件中的像素:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import random
class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'PyQt paint - tastones.com'
        self.left = 10
        self.top = 10
        self.width = 440
        self.height = 280
        self.initUI()
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        # Set window background color
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), Qt.white)
        self.setPalette(p)
        # Add paint widget and paint
        self.m = PaintWidget(self)
        self.m.move(0,0)
        self.m.resize(self.width,self.height)
        self.show()
class PaintWidget(QWidget):
   def paintEvent(self, event):
      qp = QPainter(self)
      qp.setPen(Qt.black)
      size = self.size()
      for i in range(1024):
          x = random.randint(1, size.width()-1)
          y = random.randint(1, size.height()-1)
          qp.drawPoint(x, y)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())