添加 ProgressInfoBar 组件

This commit is contained in:
2025-05-26 18:32:01 +08:00
parent 811cb9eb6b
commit e3fde36390
4 changed files with 130 additions and 6 deletions

74
ui/components/infobar.py Normal file
View File

@@ -0,0 +1,74 @@
from PySide6.QtCore import Qt
from PySide6.QtGui import QIcon
from qfluentwidgets import InfoBar, InfoBarPosition, InfoBarIcon, FluentIconBase, ProgressBar, IndeterminateProgressBar, \
InfoBarManager
class ProgressInfoBar(InfoBar):
def __init__(self, icon: InfoBarIcon | FluentIconBase | QIcon | str, title: str, content: str, orient=Qt.Horizontal,
isClosable=True, duration=1000,
position=InfoBarPosition.TOP_RIGHT, parent=None):
super().__init__(icon, title, content, orient, isClosable, duration, position, parent)
self.pb = ProgressBar()
self.ipb = IndeterminateProgressBar(start=True)
self.addWidget(self.pb)
self.addWidget(self.ipb)
self.hide_progress()
self.show()
def set_progress(self, value: int):
manager = InfoBarManager.make(self.position)
if value < 0 or value > 100:
if not self.ipb.isVisible():
manager.remove(self)
self.ipb.setVisible(True)
self.pb.setVisible(False)
self.adjustSize()
manager.add(self)
else:
return
else:
if not self.pb.isVisible():
manager.remove(self)
self.pb.setVisible(True)
self.ipb.setVisible(False)
self.adjustSize()
manager.add(self)
self.pb.setValue(value)
def hide_progress(self):
self.pb.setVisible(False)
self.ipb.setVisible(False)
self.adjustSize()
def set_title(self, title: str):
if title == self.title:
return
manager = InfoBarManager.make(self.position)
manager.remove(self)
self.title = title
self._adjustText()
manager.add(self)
def show_success(self, title: str = '成功!', content: str = "操作已完成", isCloseable: bool = True,
duration: int = 5000):
self.setVisible(False)
InfoBarManager.make(self.position).remove(self)
InfoBar.success(
title=title,
content=content,
orient=Qt.Horizontal,
isClosable=isCloseable,
position=InfoBarPosition.BOTTOM,
duration=duration,
parent=self.parent()
)

View File

@@ -4,6 +4,8 @@ from qfluentwidgets import FluentIcon, MSFluentWindow, NavigationItemPosition, M
from ui.pyui.about_ui import AboutWidget
from ui.pyui.achievement_ui import AchievementWidget
from ui.pyui.defense_ui import DefenseWidget
from ui.pyui.test_ui import TestWidget
from utils.function import is_frozen
class MainWindow(MSFluentWindow):
@@ -13,6 +15,8 @@ class MainWindow(MSFluentWindow):
self.achievementInterface = AchievementWidget('Achievement Interface', self)
self.defenseInterface = DefenseWidget('Defense Interface', self)
self.aboutInterface = AboutWidget('About Interface', self)
if not is_frozen():
self.testInterface = TestWidget('Test Interface', self)
self.achievementInterface.error.connect(self.showError)
self.defenseInterface.errorSignal.connect(self.showError)
@@ -23,6 +27,9 @@ class MainWindow(MSFluentWindow):
def initNavigation(self):
self.addSubInterface(self.achievementInterface, FluentIcon.SPEED_HIGH, '达成度')
self.addSubInterface(self.defenseInterface, FluentIcon.FEEDBACK, '答辩')
if not is_frozen():
self.addSubInterface(self.testInterface, FluentIcon.VIEW, '测试')
self.addSubInterface(self.aboutInterface, FluentIcon.INFO, '关于', position=NavigationItemPosition.BOTTOM)
def initWindow(self):

45
ui/pyui/test_ui.py Normal file
View File

@@ -0,0 +1,45 @@
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QVBoxLayout
from qfluentwidgets import PushButton, InfoBarIcon, InfoBarPosition
from ui.components.infobar import ProgressInfoBar
from ui.components.widget import Widget
class TestWidget(Widget):
def __init__(self, key: str, parents=None):
super().__init__(key, parents)
self.button = PushButton('测试按钮')
self.vbox = QVBoxLayout(self)
self.vbox.addWidget(self.button)
self.vbox.addStretch(1)
self.pib = ProgressInfoBar(
icon=InfoBarIcon.INFORMATION,
title='请稍后',
content="",
orient=Qt.Horizontal,
isClosable=False,
position=InfoBarPosition.BOTTOM,
duration=-1,
parent=self
)
self.p = -20
self.button.clicked.connect(self.add_progress)
def add_progress(self):
self.p += 10
self.pib.setVisible(True)
self.pib.set_progress(self.p)
if 0 <= self.p <= 100:
self.pib.set_title('正在制作')
elif 100 <= self.p <= 110:
self.pib.set_title('请稍后')
elif self.p > 110:
self.pib.show_success()
self.p = -20
self.pib.set_title('正在等待')

View File

@@ -176,15 +176,13 @@ def gen_picture(data: list[list[str]], kpi_number: int, start_index: int, end_in
return res
if __name__ == '__main__':
nums = [1, 2, 3, '4(1)', '42']
formatted_ranges = format_ranges(nums)
print(formatted_ranges)
def resource_path(relative_path: str) -> str:
if hasattr(sys, '_MEIPASS'):
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def is_frozen() -> bool:
return getattr(sys, 'frozen', False)