PyQt5: chapter10-以所需的字体和大小绘制文本

实例

  1. 创建基于Dialog without Buttons模板窗口
  2. 添加Label,Text Edit,Lsit Widget,Combo Box,PushButton部件各一个
  3. 设定Label的text为Enter some text in leftmost box, select font and size, and click the Draw Text button
  4. List Widget 部件添加子项目
  5. Combo Box部件添加子项目
  6. 设定Push Button的text为Draw Text
  7. 设定List Widget的objectName为listWidgetFont
  8. 设定ComboBox的objectName为comboBoxFontSize
  9. 设定PushButton的objectName为pushButtonDrawText
  10. 保存为demoDrawText.ui
  11. 使用pyuic生成demoDrawText.py
  12. 创建callDrawText.py
import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt
from cookbook_200505.demoDrawText import *
class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.pushButtonDrawText.clicked.connect(self.dispText)
        self.textToDraw=""
        self.fontName="Courier New"
        self.fontSize=5
        self.show()
    def paintEvent(self, event):
        qp = QPainter()
        qp.begin(self)
        qp.setPen(QColor(168, 34, 3))
        qp.setFont(QFont(self.fontName, self.fontSize))
        qp.drawText(event.rect(), Qt.AlignCenter,self.textToDraw)
        qp.end()
    def dispText(self):
        self.fontName=self.ui.listWidgetFont.currentItem().text()
        self.fontSize=int(self.ui.comboBoxFontSize.itemText(self.ui.comboBoxFontSize.currentIndex()))
        self.textToDraw=self.ui.textEdit.toPlainText()
        self.update()
if __name__=="__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec())

猜你喜欢

转载自blog.csdn.net/weixin_43307431/article/details/105982160
今日推荐