gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:23:56 24 4
gpt4 key购买 nike

我正在尝试添加 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com