-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
134 lines (106 loc) · 4.65 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import sys
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import QIcon, QPainter, QImage, QBrush, QColor, QFont
from PyQt5.QtWidgets import QApplication, QFrame, QStackedWidget, QHBoxLayout, QLabel
from qfluentwidgets import (NavigationInterface, NavigationItemPosition, NavigationWidget, MessageBox,
isDarkTheme, setTheme, Theme, setThemeColor)
from qfluentwidgets import FluentIcon as FIF
from qframelesswindow import FramelessWindow, StandardTitleBar
from widgets.training import TrainingWidget
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))
setTheme(Theme.AUTO)
self.hBoxLayout = QHBoxLayout(self)
self.navigationInterface = NavigationInterface(self, showMenuButton=True)
self.stackWidget = QStackedWidget(self)
# create sub interface
self.trainingInterface = TrainingWidget(self)
self.evalInterface = Widget('Music Interface', self)
self.settingInterface = Widget('Setting Interface', self)
self.stackWidget.addWidget(self.trainingInterface)
self.stackWidget.addWidget(self.evalInterface)
self.stackWidget.addWidget(self.settingInterface)
# 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):
self.navigationInterface.addItem(
routeKey=self.trainingInterface.objectName(),
icon=FIF.EDIT,
text='Training',
onClick=lambda: self.switchTo(self.trainingInterface)
)
self.navigationInterface.addItem(
routeKey=self.evalInterface.objectName(),
icon=FIF.SAVE,
text='Saved models',
onClick=lambda: self.switchTo(self.evalInterface)
)
self.navigationInterface.addItem(
routeKey=self.settingInterface.objectName(),
icon=FIF.SETTING,
text='Settings',
onClick=lambda: self.switchTo(self.settingInterface),
position=NavigationItemPosition.BOTTOM
)
# !IMPORTANT: don't forget to set the default route key if you enable the return button
# self.navigationInterface.setDefaultRouteKey(self.musicInterface.objectName())
# set the maximum width
# self.navigationInterface.setExpandWidth(300)
self.stackWidget.currentChanged.connect(self.onCurrentInterfaceChanged)
self.stackWidget.setCurrentIndex(0)
def initWindow(self):
self.resize(1200, 800)
self.setWindowIcon(QIcon('resource/logo.png'))
self.setWindowTitle('Federated GUI')
self.titleBar.setAttribute(Qt.WA_StyledBackground)
desktop = QApplication.desktop().availableGeometry()
w, h = desktop.width(), desktop.height()
self.move(w // 2 - self.width() // 2, h // 2 - self.height() // 2)
self.setQss()
def setQss(self):
color = 'dark' if isDarkTheme() else 'light'
with open(f'styles/{color}.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())
def showMessageBox(self):
w = MessageBox(
'This is a help message',
'You clicked a customized navigation widget. You can add more custom widgets by calling `NavigationInterface.addWidget()` 😉',
self
)
w.exec()
if __name__ == '__main__':
QApplication.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()