python - 如果结果为 True,如何获得绿色矩形;如果 result = (paper == dict_k) 中结果为 False,如何获得红色框?

标签 python python-3.x pyqt pyqt5

我正在尝试添加 id 文本和矩形,如果结果为 True,则添加绿色;如果结果为 false,则添加红色。我可以从实时数据中获取文本中的 id,但没有获取带有颜色的矩形。有人可以帮我解决这个问题吗?当我删除矩形 block 时,我可以获得 id 和 True/False 结果

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtCore import QTimer
import sys
from PyQt5.QtGui import QPainter, QBrush, QPen
from PyQt5.QtCore import Qt


class MainWindow(QMainWindow):
  # constructor
  def __init__(self):
    QMainWindow.__init__(self)
    # counter
    # self.i = 0
    # add QLabel
    self.top = 150
    self.left = 150
    self.width = 1000
    self.height = 1000
    self.qLbl = QLabel('Not yet initialized')
    self.setCentralWidget(self.qLbl)
    # make QTimer
    self.qTimer = QTimer()
    # set interval to 1 s
    self.qTimer.setInterval(1000) # 1000 ms = 1 s
    # connect timeout signal to signal handler
    # label.setFont(QFont('Arial', 20))
    self.setStyleSheet("QLabel {font: 16pt Arial}")
    self.qTimer.timeout.connect(self.getSensorValue)
    self.qTimer.timeout.connect(self.paintevent)

    # start timer
    self.qTimer.start()


  def getSensorValue(self):
    # self.i += 1
    # print('%d. call of getSensorValue()' % self.i)
    id = 'ID1123'
    t = neo4jconnection(paper_id=id)
    paper = neo4jresultparser(t)
    dict_k = jsonfilereader(paper_id=id)  # probably not need it
    result = (paper == dict_k)
    self.qLbl.setText((id +" "+str( result)))

def paintEvent(self, event):
    painter = QPainter(self)
    painter.setPen(QPen(Qt.green, 5, Qt.SolidLine))
    painter.setBrush(QBrush(Qt.green, Qt.SolidPattern))
    painter.drawRect(40, 40, 400, 200)


qApp = QApplication(sys.argv)
# setup GUI
qWin = MainWindow()
qWin.setGeometry(100, 100, 1000, 1000)
qWin.show()
# run application
sys.exit(qApp.exec_())

最佳答案

你不应该直接调用paintEvent,而应该通过update()或repaint()方法,另一方面,颜色必须是类的一个属性,根据result的值改变,然后调用update():

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setStyleSheet("QLabel {font: 16pt Arial}")
        self.qLbl = QLabel("Not yet initialized")
        self.setCentralWidget(self.qLbl)

        self.qTimer = QTimer()
        self.qTimer.setInterval(1000)
        self.qTimer.timeout.connect(self.getSensorValue)
        self.qTimer.start()

        self.color = QColor()

    def getSensorValue(self):
        id_ = "ID1123"
        t = neo4jconnection(paper_id=id_)
        paper = neo4jresultparser(t)
        dict_k = jsonfilereader(paper_id=id_)
        result = paper == dict_k
        self.qLbl.setText("{} {}".format(id_, result))
        self.color = QColor("green") if result else QColor("red")
        self.update()

    def paintEvent(self, event):
        if self.color.isValid():
            painter = QPainter(self)
            painter.setPen(QPen(self.color, 5, Qt.SolidLine))
            painter.setBrush(QBrush(self.color, Qt.SolidPattern))
            painter.drawRect(40, 40, 400, 200)

关于python - 如果结果为 True,如何获得绿色矩形;如果 result = (paper == dict_k) 中结果为 False,如何获得红色框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60253240/

相关文章:

python - 如何使用 matplotlib 将半极坐标图设置为北

python - 使用 Python for PyQt WebEngine 授予对 Cam & Mic 的访问权限

scroll - PyQt4 : is there any signal related to scrollbar?

python - 如何使用 xlwings 调整宽度列

python - 正则表达式中的空格

python - 具有不同初始化参数的多重继承的真正解决方案

python - 如何匹配 2 个数据框中的电子邮件地址

python - PyQt 应用程序与 sqlalchemy 数据库

python - 语句 print ='' ,end ("\t"中 end ='' 的含义)?

python - 重命名外部模块类方法