feat(account): account creation
This commit is contained in:
parent
d87b0d6d87
commit
6742c3c1a6
1 changed files with 103 additions and 0 deletions
103
src/budget/_account.py
Normal file
103
src/budget/_account.py
Normal file
|
@ -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))
|
Loading…
Reference in a new issue