import math from PyQt5.QtGui import QPixmap from PyQt5.QtWidgets import QFrame, QWidget, QPushButton import json from exceptiongroup import catch class TaskResultEmbedFrame(QFrame): def __init__(self, task, parent=None): super(TaskResultEmbedFrame, self).__init__(parent) self.task = task self.embed_buttons = [] def showEvent(self, e): params = json.loads(self.task[3]) try: max_x = math.ceil(float(max(params, key=lambda p: float(p["x"]))["x"]) / 1000) * 1000 max_y = math.ceil(float(max(params, key=lambda p: float(p["center"]))["center"])) * 1000 w = self.geometry().width() h = self.geometry().height() scale = min(max_x / w, max_y / h) for param in params: embed_button = QPushButton(param["code"], self) embed_button.setObjectName("embedButton") embed_button.setFixedSize(math.ceil(float(param["w"]) / scale), math.ceil(float(param["h"]) / scale)) left = math.ceil((float(param["x"]) - float(param["w"]) / 2) / scale) top = h - math.ceil(float(param["center"]) * 1000 / scale) embed_button.move(left, top) embed_button.setVisible(True) # p = self.grab() # p.save("d:\\demo.bmp", "bmp") except ValueError: print("任务参数有错误!") def setChecked(self, text): for btn in self.embed_buttons: if btn.text() == text: btn.setStyleSheet("border: 1px solid #6cff6c;") def setError(self, text): for btn in self.embed_buttons: if btn.text() == text: btn.setStyleSheet("border: 1px solid #FF4444;")