使用drawText()放置具有坐标的文本

2024-06-17 12:04:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用坐标来放置文本,但我不知道如何引入参数。 我在网上搜了一下,只有一些例子Qt.对齐----,如果我把参数放在文档中,就会出错。 它是用python3.6中的PyQt5构建的

import sys
from PyQt5.QtCore import QTimer, Qt, QRectF
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QColor, QFont, QPainter


class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.stopwatch()

    def stopwatch(self):

        hbox = QHBoxLayout()

        self.setLayout(hbox)
        self.setWindowTitle("Example")
        self.setGeometry(400,400,400,200)

        self.formato = "0:00.0"

        self.show()


    def paintEvent(self, event):
        qp = QPainter()
        qp.begin(self)
        self.draw_rect(event, qp)
        qp.end()

    def draw_rect(self,event, qp):
        #Black Rectangle
        col = QColor("Black")
        col.setNamedColor("Black")
        qp.setPen(col)

        qp.setBrush(QColor("Black"))
        qp.drawRect(130,000,400,200)

        #formato
        qp.setPen(QColor("Green"))
        qp.setFont(QFont('Helvetica', 48))
        qp.drawText(event.rect(), QRect(50, 50, 50, 50),5 , self.formato)  # Problem 


app = QApplication(sys.argv)
w = Window()
sys.exit(app.exec_())

请忽略其他错误,这只是我正在构建的程序的一个片段。如果这是个新手的问题,我很抱歉。在


Tags: fromrectimportselfevent参数defsys
1条回答
网友
1楼 · 发布于 2024-06-17 12:04:57

QPainter提供了几种绘制文本的选项:

void drawText(const QPointF & position, const QString & text)
void drawText(const QPoint & position, const QString & text)
void drawText(const QRectF & rectangle, int flags, const QString & text, QRectF * boundingRect = 0)
void drawText(const QRect & rectangle, int flags, const QString & text, QRect * boundingRect = 0)
void drawText(int x, int y, const QString & text)
void drawText(int x, int y, int width, int height, int flags, const QString & text, QRect * boundingRect = 0)
void drawText(const QRectF & rectangle, const QString & text, const QTextOption & option = QTextOption())

在您的情况下,您应该使用:

^{pr2}$

注意:导致窗口关闭的错误是因为您传递的参数与任何表单都不对应。在

相关问题 更多 >