Files
fluent_widgets_pyside6/main.py

180 lines
6.9 KiB
Python
Raw Normal View History

2025-08-14 18:45:16 +08:00
# coding:utf-8
import sys
from PySide6.QtCore import Qt, QRect, QUrl
from PySide6.QtGui import QIcon, QPainter, QImage, QBrush, QColor, QFont, QDesktopServices
from PySide6.QtWidgets import QApplication, QFrame, QStackedWidget, QHBoxLayout, QLabel
from qfluentwidgets import (NavigationInterface, NavigationItemPosition, NavigationWidget, MessageBox,
isDarkTheme, setTheme, Theme, setThemeColor, qrouter, FluentWindow, NavigationAvatarWidget)
from qfluentwidgets import FluentIcon as FIF
from qframelesswindow import FramelessWindow, StandardTitleBar
from app.view.system_interface import SystemInterface
from app.view.produce_interface import ProduceInterface
from app.view.text_interface import TextInterface
from app.view.data_interface import DataInterface
class Widget(QFrame):
def __init__(self, text: str, parent=None):
super().__init__(parent=parent)
self.label = QLabel(text, self)
self.label.setAlignment(Qt.AlignCenter)
self.hBoxLayout = QHBoxLayout(self)
self.hBoxLayout.addWidget(self.label, 1, Qt.AlignCenter)
self.setObjectName(text.replace(' ', '-'))
class Window(FramelessWindow):
def __init__(self):
super().__init__()
self.setTitleBar(StandardTitleBar(self))
# use dark theme mode
setTheme(Theme.DARK)
# change the theme color
# setThemeColor('#0078d4')
self.hBoxLayout = QHBoxLayout(self)
self.navigationInterface = NavigationInterface(self, showMenuButton=True)
self.stackWidget = QStackedWidget(self)
# create sub interface
# self.system = SystemInterface(self)
# self.product = ProduceInterface(self)
# self.robot = Widget('机械臂基础设置', self)
# self.io = Widget('IO面板', self)
# self.position = Widget('位置设定', self)
# self.basic = Widget('基础设置', self)
# self.point = Widget('点位调试', self)
# self.other = Widget('其他设置', self)
self.data=DataInterface(self)
# initialize layout
self.initLayout()
# add items to navigation interface
self.initNavigation()
self.initWindow()
def initLayout(self):
self.hBoxLayout.setSpacing(0)
self.hBoxLayout.setContentsMargins(0, self.titleBar.height(), 0, 0)
self.hBoxLayout.addWidget(self.navigationInterface)
self.hBoxLayout.addWidget(self.stackWidget)
self.hBoxLayout.setStretchFactor(self.stackWidget, 1)
def initNavigation(self):
# enable acrylic effect
# self.navigationInterface.setAcrylicEnabled(True)
# self.addSubInterface 加入导航栏页面
# self.addSubInterface(self.system, FIF.SETTING, '系统设置', NavigationItemPosition.SCROLL)
# self.addSubInterface(self.product, FIF.COMPLETED, '生产界面', parent=self.system)
# self.addSubInterface(self.robot, FIF.ROBOT, '机械臂基础设置', parent=self.system)
# self.addSubInterface(self.io, FIF.GAME, 'IO面板', parent=self.system)
# self.addSubInterface(self.position, FIF.IOT, '位置设定', parent=self.system)
# self.addSubInterface(self.basic, FIF.DEVELOPER_TOOLS, '基础设置', parent=self.system)
# self.addSubInterface(self.point, FIF.MOVE, '点位调试', parent=self.system)
#
# self.navigationInterface.addSeparator()
#self.addSubInterface(self.other, FIF.APPLICATION, '其他设置', NavigationItemPosition.SCROLL)
self.addSubInterface(self.data, FIF.PHOTO, '数据采集', NavigationItemPosition.SCROLL)
# add navigation items to scroll area
# for i in range(1, 21):
# self.navigationInterface.addItem(
# f'folder{i}',
# FIF.FOLDER,
# f'Folder {i}',
# lambda: print('Folder clicked'),
# position=NavigationItemPosition.SCROLL
# )
# add custom widget to bottom
self.navigationInterface.addWidget(
routeKey='avatar',
widget=NavigationAvatarWidget('zhiyiYo', 'resource/shoko.png'),
onClick=self.showMessageBox,
position=NavigationItemPosition.BOTTOM,
)
#!IMPORTANT: don't forget to set the default route key if you enable the return button
# qrouter.setDefaultRouteKey(self.stackWidget, self.musicInterface.objectName())
# set the maximum width
self.navigationInterface.setExpandWidth(220)
self.stackWidget.currentChanged.connect(self.onCurrentInterfaceChanged)
self.stackWidget.setCurrentIndex(1)
# always expand
self.navigationInterface.setCollapsible(False)
def initWindow(self):
self.resize(900, 700)
self.setWindowIcon(QIcon('resource/logo.png'))
self.setWindowTitle('密胺投料控制系统')
self.titleBar.setAttribute(Qt.WA_StyledBackground)
desktop = QApplication.screens()[0].availableGeometry()
w, h = desktop.width(), desktop.height()
self.move(w//2 - self.width()//2, h//2 - self.height()//2)
# NOTE: set the minimum window width that allows the navigation panel to be expanded
self.navigationInterface.setMinimumExpandWidth(900)
self.navigationInterface.expand(useAni=True)
self.setQss()
def addSubInterface(self, interface, icon, text: str, position=NavigationItemPosition.TOP, parent=None):
""" add sub interface """
self.stackWidget.addWidget(interface)
self.navigationInterface.addItem(
routeKey=interface.objectName(),
icon=icon,
text=text,
onClick=lambda: self.switchTo(interface),
position=position,
tooltip=text,
parentRouteKey=parent.objectName() if parent else None
)
def setQss(self):
color = 'dark' if isDarkTheme() else 'light'
with open(f'resource/{color}/demo.qss', encoding='utf-8') as f:
self.setStyleSheet(f.read())
def switchTo(self, widget):
self.stackWidget.setCurrentWidget(widget)
def onCurrentInterfaceChanged(self, index):
widget = self.stackWidget.widget(index)
self.navigationInterface.setCurrentItem(widget.objectName())
#!IMPORTANT: This line of code needs to be uncommented if the return button is enabled
# qrouter.push(self.stackWidget, widget.objectName())
def showMessageBox(self):
w = MessageBox(
'支持作者🥰',
'个人开发不易,如果这个项目帮助到了您,可以考虑请作者喝一瓶快乐水🥤。您的支持就是作者开发和维护项目的动力🚀',
self
)
w.yesButton.setText('来啦老弟')
w.cancelButton.setText('下次一定')
if w.exec():
QDesktopServices.openUrl(QUrl("https://afdian.net/a/zhiyiYo"))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec()