feat(window): add tabs

This commit is contained in:
iridiumR 2023-05-22 00:08:59 +08:00
parent a6af114ad8
commit 2fa863436a
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8
4 changed files with 121 additions and 48 deletions

View file

@ -11,4 +11,5 @@ class MainWindow(QMainWindow):
self.initUI()
from ._mw import initUI
from ._pg import connectToDatabase, disconnectFromDatabase, onConnectClicked
from ._pg import connectToDatabase, disconnectFromDatabase, onConnectClicked, initConnectTab
from ._category import initCategorytTab, onCategoryAddClicked

63
src/budget/_category.py Normal file
View file

@ -0,0 +1,63 @@
from PyQt6.QtWidgets import *
def initCategorytTab(self):
# 创建连接信息控件
self.categoryComboBox = QComboBox()
self.categoryAddButton = QPushButton('Add New Category')
self.categoryAddButton.clicked.connect(self.onCategoryAddClicked)
# 创建控件布局
categoryLayout = QHBoxLayout()
categoryLayout.addWidget(self.categoryComboBox)
categoryLayout.addWidget(self.categoryAddButton)
# 连接信号槽
# self.connectButton.clicked.connect(self.onConnectClicked)
categoryWidget = QWidget()
categoryWidget.setLayout(categoryLayout)
return categoryWidget
def onCategoryAddClicked(self):
try:
if (self.cur is None):
raise Exception('Not connected to database')
# 创建添加数据对话框
addDialog = QDialog(self)
addDialog.setWindowTitle('Add New Category')
addDialog.resize(400, 300)
label1 = QLabel("Category Name:")
line1 = QLineEdit()
label2 = QLabel("Category Type")
line2 =QComboBox()
line2.addItem("Income")
line2.addItem("Expense")
button1 = QPushButton("Confirm")
button2 = QPushButton("Abort")
button1.clicked.connect(addDialog.accept)
button2.clicked.connect(addDialog.reject)
layout = QFormLayout()
layout.addRow(label1,line1)
layout.addRow(label2,line2)
layout.addRow(button1,button2)
addDialog.setLayout(layout)
# 显示添加数据对话框
if addDialog.exec() == QDialog.DialogCode.Accepted:
try:
print("success")
except Exception as e:
# 处理插入数据错误
print(e)
QMessageBox.critical(self, 'Error', str(e))
except Exception as e:
# 处理获取表格列名错误
print(e)
QMessageBox.critical(self, 'Error', str(e))

View file

@ -1,56 +1,18 @@
from PyQt6.QtWidgets import *
from ._pg import initConnectTab
from ._category import initCategorytTab
def initUI(self):
self.setWindowTitle('budget')
self.setGeometry(100, 100, 800, 600)
# 创建连接信息控件
self.hostLabel = QLabel('Host:')
self.hostLineEdit = QLineEdit()
self.hostLineEdit.setText('localhost')
connectTab = initConnectTab(self)
categoryTab = initCategorytTab(self)
self.portLabel = QLabel('Port:')
self.portLineEdit = QLineEdit()
self.portLineEdit.setText('5432')
self.dbNameLabel = QLabel('Database:')
self.dbNameLineEdit = QLineEdit()
self.dbNameLineEdit.setText('budget')
self.userLabel = QLabel('Username:')
self.userLineEdit = QLineEdit()
self.userLineEdit.setText('postgres')
self.passwordLabel = QLabel('Password:')
self.passwordLineEdit = QLineEdit()
self.passwordLineEdit.setEchoMode(QLineEdit.EchoMode.Password)
self.passwordLineEdit.setText('')
self.connectButton = QPushButton('Connect')
self.connectButton.clicked.connect(self.onConnectClicked)
# 创建连接信息控件布局
connectLayout = QHBoxLayout()
connectLayout.addWidget(self.hostLabel)
connectLayout.addWidget(self.hostLineEdit)
connectLayout.addWidget(self.portLabel)
connectLayout.addWidget(self.portLineEdit)
connectLayout.addWidget(self.dbNameLabel)
connectLayout.addWidget(self.dbNameLineEdit)
connectLayout.addWidget(self.userLabel)
connectLayout.addWidget(self.userLineEdit)
connectLayout.addWidget(self.passwordLabel)
connectLayout.addWidget(self.passwordLineEdit)
connectLayout.addWidget(self.connectButton)
connectWidget = QWidget()
connectWidget.setLayout(connectLayout)
# 创建主窗口布局
mainLayout = QVBoxLayout()
mainLayout.addWidget(connectWidget)
mainWidget = QWidget()
mainWidget.setLayout(mainLayout)
# 创建主窗口tab布局
mainWidget = QTabWidget()
mainWidget.addTab(connectTab, 'Connect')
mainWidget.addTab(categoryTab, 'Category')
self.setCentralWidget(mainWidget)

View file

@ -35,3 +35,50 @@ def onConnectClicked(self):
else:
self.disconnectFromDatabase()
self.connectButton.setText('Connect')
def initConnectTab(self):
# 创建连接信息控件
self.hostLabel = QLabel('Host:')
self.hostLineEdit = QLineEdit()
self.hostLineEdit.setText('159.75.75.169')
self.portLabel = QLabel('Port:')
self.portLineEdit = QLineEdit()
self.portLineEdit.setText('5432')
self.dbNameLabel = QLabel('Database:')
self.dbNameLineEdit = QLineEdit()
self.dbNameLineEdit.setText('budget')
self.userLabel = QLabel('Username:')
self.userLineEdit = QLineEdit()
self.userLineEdit.setText('budget')
self.passwordLabel = QLabel('Password:')
self.passwordLineEdit = QLineEdit()
self.passwordLineEdit.setEchoMode(QLineEdit.EchoMode.Password)
self.passwordLineEdit.setText('budget')
self.connectButton = QPushButton('Connect')
# 连接信号槽
self.connectButton.clicked.connect(self.onConnectClicked)
# 创建连接信息控件布局
connectLayout = QHBoxLayout()
connectLayout.addWidget(self.hostLabel)
connectLayout.addWidget(self.hostLineEdit)
connectLayout.addWidget(self.portLabel)
connectLayout.addWidget(self.portLineEdit)
connectLayout.addWidget(self.dbNameLabel)
connectLayout.addWidget(self.dbNameLineEdit)
connectLayout.addWidget(self.userLabel)
connectLayout.addWidget(self.userLineEdit)
connectLayout.addWidget(self.passwordLabel)
connectLayout.addWidget(self.passwordLineEdit)
connectLayout.addWidget(self.connectButton)
connectWidget = QWidget()
connectWidget.setLayout(connectLayout)
return connectWidget