Him_Hei 2024-09-06 10:47 采纳率: 53.3%
浏览 23

PySide6无报错退出代码1,如何解决?

代码如下,继承自PropertyThread重写run函数,在run内调用ProgressBarSetText等函数直接退出代码1,没有任何错误信息。但在debug模式下什么事都没有!!


from typing import Union, Literal
from PySide6 import QtWidgets, QtCore, QtGui
from DataBaseManage import DIGMSGX5

WARNINGCOLOR = r"rgb(247,182,30)"
ERRORCOLOR = r"rgb(200,0,0)"
RunTime = 100


class ProgressBar(QtWidgets.QWidget):

    def setupUi(self) -> None:
        self.MainLayout: QtWidgets.QHBoxLayout = QtWidgets.QHBoxLayout(self)
        self.ProgressBar: QtWidgets.QWidget = QtWidgets.QWidget(self)
        self.ProgressBar.setStyleSheet(self.ProgressBarStyleSheet)
        self.ProgressBarLayout: QtWidgets.QVBoxLayout = QtWidgets.QVBoxLayout(self.ProgressBar)
        self.ProgressBarLayout.setSpacing(0)
        self.ProgressBarLayout.setContentsMargins(0, 0, 0, 0)
        self.MainLayout.setContentsMargins(0, 0, 0, 0)
        self.Chunk: QtWidgets.QLabel = QtWidgets.QLabel(self.ProgressBar)
        self.Chunk.setStyleSheet(self.ProgressBarChunkStyleSheet)
        self.ProgressBarLayout.addWidget(self.Chunk)
        self.MainLayout.addWidget(self.ProgressBar)
        self.setMaximumSize(9999, 4)

    def setChunkBusyDuration(self, duration: int) -> None:
        self.ChunkBusyAnima.setDuration(duration)

    def __init__(self, parent=None):
        super(ProgressBar, self).__init__(parent)
        self.ProgressBarStyleSheet: str = u"background-color: rgba(32,56,100,100);border-radius:2px;"
        self.ProgressBarChunkStyleSheet: str = u"background-color: rgb(32,56,100);border-radius:2px;min-width:4px"
        # self.ProgressBarChunkStyleSheet: str = (
        #     u"background-color: qlineargradient(spread:pad, x1:1, y1:0.75, x2:0, y2:0.25, "
        #     u"stop:0 rgba(1, 161, 179, 255), stop:0.488636 rgba(31, 71, 205, 255), "
        #     u"stop:1 rgba(111, 11, 222, 255));"
        #     u"border-radius:2px;min-width:4px")
        self.setupUi()
        self.BusyFlag: bool = False

        self.ChunkBusyAnima: QtCore.QPropertyAnimation = QtCore.QPropertyAnimation(self.Chunk, b"pos")
        self.ChunkBusyAnimaRunFlag: bool = False
        self.setChunkBusyDuration(1000)
        self.ChunkBusyAnima.finished.connect(self.__busyAnimaEnd)

        self.ChunkIndexAnima: QtCore.QPropertyAnimation = QtCore.QPropertyAnimation(self.Chunk, b"size")
        self.ChunkIndexAnima.setDuration(100)
        self.ChunkIndexAnima.finished.connect(self.__chunkIndexAnimaEnd)
        self.ChunkIndexAnimaRunFlag: bool = False

        self.MaxIndex: int = 0
        self.MinIndex: int = 0
        self.setBorderWidth(0)
        self.TaskItem: list[Union[QtCore.QSize, -1]] = []

        self.timer: QtCore.QTimer = QtCore.QTimer()
        self.timer.timeout.connect(self.scan)
        self.timer.setInterval(30)
        self.timer.start()

    def __busyAnimaEnd(self):
        if self.ChunkBusyAnimaRunFlag:
            self.ChunkBusyAnima.stop()
            self.ChunkBusyAnimaRunFlag: bool = False
        if self.BusyFlag:
            self.__busyAnimaStart()
            return
        self.Chunk.setGeometry(0, 0, 4, 4)
        self.setValue(0)

    def __busyAnimaStart(self) -> None:
        if self.ChunkBusyAnimaRunFlag:
            return
        self.__chunkIndexAnimaEnd()
        self.setValue(int(self.MaxIndex / 4))
        if self.Chunk.width() != int(self.ProgressBar.width() / 4):
            self.Chunk.setGeometry(QtCore.QRect(0, 0, int(self.ProgressBar.width() / 4), self.Chunk.height()))
        if self.Chunk.pos().x() != (self.ProgressBar.width() - self.Chunk.width() - self.BorderWidth):
            endPos = QtCore.QPoint(self.ProgressBar.width() - self.Chunk.width() - self.BorderWidth, self.Chunk.y())
        else:
            endPos = QtCore.QPoint(0, self.Chunk.y())
        self.ChunkBusyAnima.setEndValue(endPos)
        self.BusyFlag: bool = True
        self.ChunkBusyAnimaRunFlag: bool = True
        self.ChunkBusyAnima.start()

    def __chunkIndexAnimaEnd(self) -> None:
        if self.ChunkIndexAnimaRunFlag:
            self.ChunkIndexAnima.stop()
        self.ChunkIndexAnimaRunFlag: bool = False

    def scan(self) -> None:
        self._Index()

    def _Index(self) -> None:
        if self.TaskItem and not self.ChunkIndexAnimaRunFlag:
            _ = self.TaskItem[-1]
            if _ == -1:
                self.__busyAnimaStart()
                self.TaskItem.clear()
                return
            self.Chunk.setGeometry(0, 0, self.Chunk.width(), 4)
            self.ChunkIndexAnima.setEndValue(_)
            self.TaskItem.clear()
            self.ChunkIndexAnimaRunFlag: bool = True
            self.ChunkIndexAnima.start()
        return

    def setValue(self, value: int) -> None:
        if not self.BusyFlag and self.MaxIndex > 0:
            _in: float = (self.ProgressBar.width() - self.BorderWidth - 1) / self.MaxIndex
            self.TaskItem.append(QtCore.QSize(int(_in * value), self.Chunk.height()))

    def inBysh(self) -> None:
        self.TaskItem.append(-1)

    def inNormal(self, RangeStart: int, RangeEnd: int) -> None:
        if RangeEnd <= 0 or RangeStart >= RangeEnd:
            return
        self.BusyFlag: bool = False
        self.__busyAnimaEnd()
        self.MaxIndex: int = RangeEnd
        self.MinIndex: int = RangeStart

    def setBorderWidth(self, width: int) -> None:
        self.BorderWidth: int = width
        self.ProgressBarLayout.setContentsMargins(width, width, width, width)


class ThreadView(QtWidgets.QFrame):
    @QtCore.Slot()
    def Busy(self) -> None:
        self.__ProgressBar.setChunkBusyDuration(1000)
        self.__ProgressBar.inBysh()

    @QtCore.Slot(int, int)
    def Normal(self, a0: int, a1: int) -> None:
        self.__ProgressBar.inNormal(a0, a1)

    @QtCore.Slot(str)
    def setText(self, text: str) -> None:
        self.__MsgLabel.setText(text)

    @QtCore.Slot(int)
    def setValue(self, index: int) -> None:
        self.__ProgressBar.setValue(index)

    def __init__(self):
        super().__init__()
        self.setMaximumHeight(31)
        self.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.setFrameShadow(QtWidgets.QFrame.Raised)
        self.__Layout: QtWidgets.QVBoxLayout = QtWidgets.QVBoxLayout(self)
        self.__Layout.setContentsMargins(5, 5, 5, 5)
        self.__MsgLabel: QtWidgets.QLineEdit = QtWidgets.QLineEdit(self)
        self.__MsgLabel.setMaxLength(500)
        self.__MsgLabel.setReadOnly(True)
        self.__MsgLabel.setStyleSheet(
            """
            font-family: JetBrains Mono Light;
            font-size: 8pt;
            font-weight: normal;
            """
        )
        self.__ProgressBar: ProgressBar = ProgressBar(self)
        self.__Layout.addWidget(self.__ProgressBar)
        self.__Layout.addWidget(self.__MsgLabel)
        self.fontFamily: QtGui.QFontDatabase.applicationFontFamilies = QtGui.QFontDatabase.applicationFontFamilies(
            QtGui.QFontDatabase.addApplicationFont(r":/font/JetBrainsMono-Light.ttf"))[0]
        self.__MsgLabel.setFont(QtGui.QFont(self.fontFamily, 6))


class PropertyThread(QtCore.QThread):

    def __init__(self) -> None:
        super().__init__()
        self.RunningFlag: bool = False
        self.__Msg: str = ""
        self.__UI_View = ThreadView()

    def ProgressBarClear(self):
        self.__UI_View.setValue(0)

    def close(self):
        self.View.close()

    @classmethod
    def DialogShow(cls, msg: str, level: Literal["INFO", "WARNING", "ERROR"] = "INFO", AutoClose: str = "-1") -> None:
        DataBase = DIGMSGX5(r".\RegularData.db")
        DataBase.AddDialogBox(msg, level, "N", AutoClose)

    def DialogExec(self, msg: str, level: Literal["INFO", "WARNING", "ERROR"]):
        DataBase = DIGMSGX5(r".\RegularData.db")
        ObjectFlag: str = DataBase.AddDialogBox(msg, level, "Y")
        Request = DataBase.GetDialogACTInfo(ObjectFlag)[3]
        while not (Request in ["ACCEPT", "REJECT"]):
            self.msleep(500)
            Request = DataBase.GetDialogACTInfo(ObjectFlag)[3]
        DataBase.UpdateDialogBoxMsg(ObjectFlag, "Finish")
        if Request == "ACCEPT":
            return True
        return False

    @property
    def View(self) -> QtWidgets.QFrame:
        return self.__UI_View

    def ProgressBarSetValue(self, a0: int) -> None:
        self.__UI_View.setValue(a0)

    def ProgressBarNormal(self, a0: int, a1: int) -> None:
        self.__UI_View.Normal(a0, a1)

    def ProgressBarSetText(self, msg: str) -> None:
        msg: str = str(msg)
        if self.__Msg != msg:
            self.__Msg: str = msg
            self.__UI_View.setText(msg)

    def ProgressBarBusy(self) -> None:
        self.__UI_View.Busy()

    def start(self, priority=...) -> None:
        self.setRunningFlag(True)
        super().start()

    def setRunningFlag(self, a0: bool) -> None:
        self.RunningFlag: bool = a0


  • 写回答

1条回答 默认 最新

  • 一轮明月照丘壑 2024-09-07 07:31
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    问题描述中提到在无报错退出代码为1的情况,通常在Python中,退出码为0表示正常退出,非零值表示出错。在您的情况下,退出码为1可能意味着程序中存在未被捕获的异常,导致程序异常终止。这种异常可能在PySide6的某个部分或者其他依赖库中。

    由于没有具体的错误信息和完整的代码执行流程,我只能给出一些通用的建议:

    1. 检查异常处理:请确保您的程序中包含了适当的异常处理机制,以便捕获并处理任何可能出现的异常。可以使用try-except块来捕获和处理异常。
    2. 使用调试工具:在调试模式下运行程序,查看是否有任何错误消息或异常被抛出。使用调试工具(如PyCharm的调试功能)可以帮助您定位问题所在。
    3. 检查线程使用:在您的代码中,使用了多线程(PropertyThread),请确保线程的正确使用和同步。线程间的竞争条件或死锁可能导致程序意外终止。
    4. 检查外部库和依赖:确保您使用的所有外部库和依赖都是最新的,并且与您的PySide6版本兼容。
    5. 简化问题:尝试简化您的代码,只保留最基本的功能,并逐步添加更多功能,以便更容易找到问题所在。

    如果以上建议无法解决问题,建议您提供更详细的错误信息或代码执行流程,以便更准确地定位问题所在。

    评论

报告相同问题?

问题事件

  • 创建了问题 9月6日