代码如下,继承自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