feat: more classes

change pg and category to class
This commit is contained in:
iridiumR 2023-05-22 11:02:33 +08:00
parent b12807031a
commit ce1c8c6df8
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8
5 changed files with 171 additions and 106 deletions

View file

@ -1,15 +1,13 @@
from PyQt6.QtWidgets import * from PyQt6.QtWidgets import *
from PyQt6.QtCore import * from PyQt6.QtCore import *
class MainWindow(QMainWindow): from ._pg import PostgresTab
cur = None class MainWindow(QMainWindow):
conn = None
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.initUI() self.initUI()
from ._mw import initUI from ._mw import initUI, onTabChanged
from ._pg import connectToDatabase, disconnectFromDatabase, onConnectClicked, initConnectTab from ._category import CategoryTab
from ._category import initCategorytTab, onCategoryAddClicked

View file

@ -1,7 +1,16 @@
from PyQt6.QtWidgets import * from PyQt6.QtWidgets import *
from ._tab import TabPage
from ._pg import PostgresTab
import json import json
def initCategorytTab(self): class CategoryTab(TabPage):
def __init__(self, parent):
super().__init__()
self.initUI()
self.pg = parent.pg
self.rows = []
def initUI(self):
# 创建连接信息控件 # 创建连接信息控件
self.categoryComboBox = QComboBox() self.categoryComboBox = QComboBox()
@ -13,18 +22,12 @@ def initCategorytTab(self):
categoryLayout.addWidget(self.categoryComboBox) categoryLayout.addWidget(self.categoryComboBox)
categoryLayout.addWidget(self.categoryAddButton) categoryLayout.addWidget(self.categoryAddButton)
# 连接信号槽
# self.connectButton.clicked.connect(self.onConnectClicked)
categoryWidget = QWidget() categoryWidget = QWidget()
categoryWidget.setLayout(categoryLayout) self.setLayout(categoryLayout)
return categoryWidget
def onCategoryAddClicked(self): def onCategoryAddClicked(self):
try:
if (self.cur is None):
raise Exception('Not connected to database')
# 创建添加数据对话框 # 创建添加数据对话框
addDialog = QDialog(self) addDialog = QDialog(self)
@ -76,14 +79,29 @@ def onCategoryAddClicked(self):
data = {"name": line1.text(), "income_type": type, "description": description} data = {"name": line1.text(), "income_type": type, "description": description}
data = json.dumps(data) data = json.dumps(data)
print("insert data:",data) print("insert data:",data)
self.cur.execute("INSERT INTO category (meta) VALUES (%s)", (data,))
self.conn.commit() self.pg.execute("INSERT INTO category (meta) VALUES (%s)", (data,))
# 刷新表格
self.selected()
except Exception as e: except Exception as e:
# 处理插入数据错误 # 处理插入数据错误
print(e) print(e)
QMessageBox.critical(self, 'Error', str(e)) QMessageBox.critical(self, 'Error', str(e))
def selected(self):
# 清空表格
self.categoryComboBox.clear()
# 获取表格列名
try:
self.pg.execute("SELECT c_id, meta ->> 'name' FROM category ORDER BY c_id")
self.rows = self.pg.cur.fetchall()
print(self.rows)
for row in self.rows:
data = row[1]
self.categoryComboBox.addItem(data)
except Exception as e: except Exception as e:
# 处理获取表格列名错误
print(e) print(e)
QMessageBox.critical(self, 'Error', str(e)) QMessageBox.critical(self, 'Error', str(e))

View file

@ -1,18 +1,27 @@
from PyQt6.QtWidgets import * from PyQt6.QtWidgets import *
from ._pg import initConnectTab from ._pg import PostgresTab
from ._category import initCategorytTab from ._category import CategoryTab
from ._pg import PostgresTab
def initUI(self): def initUI(self):
self.setWindowTitle('budget') self.setWindowTitle('budget')
self.setGeometry(100, 100, 800, 600) self.setGeometry(100, 100, 800, 600)
connectTab = initConnectTab(self) self.pg = PostgresTab()
categoryTab = initCategorytTab(self) self.categoryTab = CategoryTab(self)
# 创建主窗口tab布局 # 创建主窗口tab布局
mainWidget = QTabWidget() mainWidget = QTabWidget()
mainWidget.addTab(connectTab, 'Connect') mainWidget.addTab(self.pg, 'Connect')
mainWidget.addTab(categoryTab, 'Category') mainWidget.addTab(self.categoryTab, 'Category')
mainWidget.currentChanged.connect(self.onTabChanged)
self.setCentralWidget(mainWidget) self.setCentralWidget(mainWidget)
self.pg.onConnectClicked()
def onTabChanged(self, index):
if (index == 1):
self.centralWidget().widget(index).selected()

View file

@ -1,5 +1,37 @@
from PyQt6.QtWidgets import * from PyQt6.QtWidgets import *
import psycopg2 import psycopg2
from ._tab import TabPage
class PostgresTab(TabPage):
def __init__(self):
super().__init__()
self.initUI()
cur = None
conn = None
def connected(self):
return self.cur is not None
def execute(self,text,values=None):
try:
if (self.cur is None):
raise Exception('Not connected to database')
self.cur.execute(text,values)
self.conn.commit()
except Exception as e:
print(e)
QMessageBox.critical(self, 'Error', str(e))
def fetchall(self):
try:
if (self.cur is None):
raise Exception('Not connected to database')
return self.cur.fetchall()
except Exception as e:
print(e)
QMessageBox.critical(self, 'Error', str(e))
def connectToDatabase(self): def connectToDatabase(self):
try: try:
@ -37,7 +69,7 @@ def onConnectClicked(self):
self.connectButton.setText('Connect') self.connectButton.setText('Connect')
def initConnectTab(self): def initUI(self):
# 创建连接信息控件 # 创建连接信息控件
self.hostLabel = QLabel('Host:') self.hostLabel = QLabel('Host:')
self.hostLineEdit = QLineEdit() self.hostLineEdit = QLineEdit()
@ -79,6 +111,4 @@ def initConnectTab(self):
connectLayout.addWidget(self.passwordLineEdit) connectLayout.addWidget(self.passwordLineEdit)
connectLayout.addWidget(self.connectButton) connectLayout.addWidget(self.connectButton)
connectWidget = QWidget() self.setLayout(connectLayout)
connectWidget.setLayout(connectLayout)
return connectWidget

10
src/budget/_tab.py Normal file
View file

@ -0,0 +1,10 @@
from PyQt6.QtWidgets import *
import psycopg2
class TabPage(QWidget):
def __init__(self):
super().__init__()
def selected(self):
pass