import sys
import sqlite3
from dotenv import load_dotenv
from os import getenv
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
#读取配置信息
load_dotenv()
#主窗口
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initializeUI()
def initializeUI(self):
# 设置窗口标题和初始大小
self.setWindowTitle(getenv("APP_NAME"))
self.setGeometry(100, 100, 600, 400)
# 创建菜单栏对象
menubar = QMenuBar(self)
self.setMenuBar(menubar) # 将菜单栏设置到窗口中
# 创建菜单对象
fileMenu = menubar.addMenu('文件(&F)')
editMenu = menubar.addMenu('编辑(&E)')
helpMenu = menubar.addMenu('帮助(&H)')
# 创建动作(Action)并添加到菜单中
exitAction = QAction(QIcon('exit.png'), '退出', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('退出应用程序')
#exitAction.triggered.connect(qApp.quit)
exitAction.triggered.connect(self.opensecond)
fileMenu.addAction(exitAction) # 将动作添加到文件菜单中
# 添加其他动作到编辑和帮助菜单中(示例)
newAction = QAction(QIcon('new.png'), '新建', self) # 假设你有一个new.png图标文件
newAction.setShortcut('Ctrl+N')
newAction.setStatusTip('新建文件')
editMenu.addAction(newAction)
aboutAction = QAction('关于', self)
aboutAction.setStatusTip('关于本应用')
helpMenu.addAction(aboutAction)
windowList = []
def opensecond(self):
the_window =SecondWindow()
self.windowList.append(the_window)
self.close()
the_window.show()
#第二个窗口
class SecondWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('第二主界面')
self.label = QLabel()
self.label.setText("第二个主界面")
self.label.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
self.label.setAlignment(Qt.AlignCenter)
self.label.setFont(QFont("Roman times", 50, QFont.Bold))
self.setCentralWidget(self.label)
self.statusBar().showMessage("当前用户:一心狮")
self.showMaximized()
windowList = []
#重写关闭返回当前窗口
def closeEvent(self, event):
the_window = MainWindow()
self.windowList.append(the_window)
the_window.show()
event.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())