From 6742c3c1a67e8f8cdeff1189890e76bf2f1cc501 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Mon, 22 May 2023 11:35:38 +0800 Subject: [PATCH] feat(account): account creation --- src/budget/_account.py | 103 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/budget/_account.py diff --git a/src/budget/_account.py b/src/budget/_account.py new file mode 100644 index 0000000..cfbcd12 --- /dev/null +++ b/src/budget/_account.py @@ -0,0 +1,103 @@ +from PyQt6.QtWidgets import * +from PyQt6.QtGui import QDoubleValidator +from ._tab import TabPage +from ._pg import PostgresTab +import json + +class AccountTab(TabPage): + def __init__(self, parent): + super().__init__() + self.initUI() + self.pg = parent.pg + self.rows = [] + + def initUI(self): + # 创建连接信息控件 + self.accountComboBox = QComboBox() + + self.accountAddButton = QPushButton('Add New account') + self.accountAddButton.clicked.connect(self.onaccountAddClicked) + + # 创建控件布局 + accountLayout = QHBoxLayout() + accountLayout.addWidget(self.accountComboBox) + accountLayout.addWidget(self.accountAddButton) + + accountWidget = QWidget() + self.setLayout(accountLayout) + + def onaccountAddClicked(self): + + # 创建添加数据对话框 + addDialog = QDialog(self) + addDialog.setWindowTitle('Add New Account') + addDialog.resize(400, 300) + + label1 = QLabel("Account Name:") + line1 = QLineEdit() + label2 = QLabel("Account Balance:") + line2 = QLineEdit() + line2.setText("0.00") + # 只允许输入两位小数 + line2.setValidator(QDoubleValidator(0.00, 999999999.99, 2)) + label3 = QLabel("Description:") + line3 = QLineEdit() + + button1 = QPushButton("Confirm") + button2 = QPushButton("Abort") + buttonLayout = QHBoxLayout() + buttonLayout.addWidget(button1) + buttonLayout.addWidget(button2) + + button1.clicked.connect(addDialog.accept) + button2.clicked.connect(addDialog.reject) + + layout = QFormLayout() + layout.addRow(label1,line1) + layout.addRow(label2,line2) + layout.addRow(label3,line3) + layout.addRow(buttonLayout) + + addDialog.setLayout(layout) + + # 显示添加数据对话框 + if addDialog.exec() == QDialog.DialogCode.Accepted: + try: + # 获取输入数据 + name = line1.text() + balance = float(line2.text()) + + description = line3.text() + if (name == ''): + raise Exception('Account name cannot be empty') + + # JSONB插入数据 + data = {"name": line1.text(), "balance": balance, "description": description} + data = json.dumps(data) + print("insert data:",data) + + self.pg.execute("INSERT INTO account (meta) VALUES (%s)", (data,)) + + # 刷新表格 + self.selected() + + except Exception as e: + # 处理插入数据错误 + print(e) + QMessageBox.critical(self, 'Error', str(e)) + + + def selected(self): + # 清空表格 + self.accountComboBox.clear() + # 获取表格列名 + try: + self.pg.execute("SELECT a_id, meta ->> 'name' FROM account ORDER BY a_id") + self.rows = self.pg.cur.fetchall() + print(self.rows) + for row in self.rows: + data = row[1] + self.accountComboBox.addItem(data) + except Exception as e: + print(e) + QMessageBox.critical(self, 'Error', str(e)) \ No newline at end of file