首页 > 其他分享 >PyQT5之QPainter

PyQT5之QPainter

时间:2024-06-13 17:55:58浏览次数:17  
标签:__ qp self PyQT5 PyQt5 import Drawing QPainter

参考https://www.cnblogs.com/jgg54335/p/14904853.html

实例一:

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt


class Drawing(QWidget):
    def __init__(self, parent=None):
        super(Drawing, self).__init__(parent)
        self.setWindowTitle('在窗口绘制文字')
        self.resize(300, 200)
        self.text = '欢迎学习 PyQt5'

    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self)
        # 自定义绘制方法
        self.drawText(event, painter)
        painter.end()

    def drawText(self, event, qp):
        # 设置画笔的颜色
        qp.setPen(QColor(168, 34, 3))
        # 设置字体
        qp.setFont(QFont('SimSun', 20))
        # 绘制文字
        qp.drawText(event.rect(), Qt.AlignCenter, self.text)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = Drawing()
    demo.show()
    sys.exit(app.exec_())


实例二:

import sys, math
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class Drawing(QWidget):
    def __init__(self, parent=None):
        super(Drawing, self).__init__(parent)
        self.resize(300, 200)
        self.setWindowTitle('在窗口画点')

    def paintEvent(self, event):
        # 初始化绘图工具
        qp = QPainter()
        # 开始在窗口绘制
        qp.begin(self)
        # 自定义画点方法
        self.drawPoints(qp)
        # 结束在窗口的绘制
        qp.end()

    def drawPoints(self, qp):
        qp.setPen(Qt.red)
        size = self.size()

        for i in range(1000):
            # 绘制郑玄函数图像,它的周期是【-100,100】
            x = 100 * (-1 + 2.0 * i / 1000) + size.width() / 2.0
            y = -50 * math.sin((x - size.width() / 2.0) * math.pi / 50) + size.height() / 2.0

            qp.drawPoint(x, y)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = Drawing()
    demo.show()
    sys.exit(app.exec_())

实例三:

标签:__,qp,self,PyQT5,PyQt5,import,Drawing,QPainter
From: https://www.cnblogs.com/jackchen28/p/18246442

相关文章

  • PyQT5之QFontDialog
    importsysfromPyQt5.QtCoreimport*fromPyQt5.QtWidgetsimport*fromPyQt5.QtGuiimport*classFontDialogdemo(QWidget):def__init__(self,parent=None):super(FontDialogdemo,self).__init__(parent)self.resize(500,300)......
  • PyQT5之QDialog
    importsysfromPyQt5.QtCoreimport*fromPyQt5.QtGuiimport*fromPyQt5.QtWidgetsimport*classQDialogDemo(QMainWindow):def__init__(self):super().__init__()self.initUI()definitUI(self):self.setWindowTitle(�......
  • PyQT5之多页面切换
    fromPyQt5.QtWidgetsimportQApplication,QWidget,QStackedLayout,QVBoxLayoutfromPyQt5.QtWidgetsimportQPushButton,QComboBox,QSizePolicyimportsysif__name__=='__main__':app=QApplication(sys.argv)my_widget=QWidget()my_w......
  • 【Python】成功解决ModuleNotFoundError: No module named ‘PyQt5‘
    【Python】成功解决ModuleNotFoundError:Nomodulenamed‘PyQt5’ 下滑即可查看博客内容......
  • PyQT5之QSS基础/子控件选择器
    fromPyQt5.QtWidgetsimport*importsysclassBasicQCSS(QWidget):def__init__(self):super().__init__()self.setWindowTitle("QSS样式/子控件选择器")btn1=QPushButton(self)btn1.setText("按钮1")b......
  • PyQT5窗口样式设置
    importsysfromPyQt5.QtCoreimport*fromPyQt5.QtGuiimport*fromPyQt5.QtWidgetsimport*classWindowPattern(QMainWindow):def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)self.resize(500,300)self......
  • PyQT5之设置窗口控件风格
    设置窗口控件风格QApplication.setStyle(...)窗口可以显示三种风格:['windowsvista','Windows','Fusion']importsysfromPyQt5.QtCoreimport*fromPyQt5.QtGuiimport*fromPyQt5.QtWidgetsimport*#print(QStyleFactory.keys())classWindow......
  • PyQT5之多窗口交互
    方法一:不适用信号与槽MultiWin.py文件"""多窗口交互--不适用信号与槽"""importsysfromPyQt5.QtCoreimport*fromPyQt5.QtGuiimport*fromPyQt5.QtWidgetsimport*fromDateDialogimportDateDialogclassMultiWindow1(QWidget):def__init__......
  • PyQT5之为槽函数传递参数
    方法一:lambda表达式传递参数fromPyQt5.QtCoreimport*fromPyQt5importQtCorefromPyQt5.QtWidgetsimport*importsysclassLambdaSlotArg(QMainWindow):def__init__(self):super().__init__()self.setWindowTitle("使用Lambda表达式为槽函......
  • PyQT5信号与槽的连接
    方法一:fromPyQt5.QtCoreimport*fromPyQt5.QtWidgetsimport*importsysclassAutoSignalSlot(QWidget):def__init__(self):super().__init__()self.setWindowTitle("信号与槽自动连接")self.resize(300,100)self.okB......