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.QtCore import *
class MainWindow(QMainWindow):
from ._pg import PostgresTab
cur = None
conn = None
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
from ._mw import initUI
from ._pg import connectToDatabase, disconnectFromDatabase, onConnectClicked, initConnectTab
from ._category import initCategorytTab, onCategoryAddClicked
from ._mw import initUI, onTabChanged
from ._category import CategoryTab

View file

@ -1,30 +1,33 @@
from PyQt6.QtWidgets import *
from ._tab import TabPage
from ._pg import PostgresTab
import json
def initCategorytTab(self):
# 创建连接信息控件
self.categoryComboBox = QComboBox()
class CategoryTab(TabPage):
def __init__(self, parent):
super().__init__()
self.initUI()
self.pg = parent.pg
self.rows = []
self.categoryAddButton = QPushButton('Add New Category')
self.categoryAddButton.clicked.connect(self.onCategoryAddClicked)
def initUI(self):
# 创建连接信息控件
self.categoryComboBox = QComboBox()
# 创建控件布局
categoryLayout = QHBoxLayout()
categoryLayout.addWidget(self.categoryComboBox)
categoryLayout.addWidget(self.categoryAddButton)
self.categoryAddButton = QPushButton('Add New Category')
self.categoryAddButton.clicked.connect(self.onCategoryAddClicked)
# 连接信号槽
# self.connectButton.clicked.connect(self.onConnectClicked)
# 创建控件布局
categoryLayout = QHBoxLayout()
categoryLayout.addWidget(self.categoryComboBox)
categoryLayout.addWidget(self.categoryAddButton)
categoryWidget = QWidget()
categoryWidget.setLayout(categoryLayout)
return categoryWidget
categoryWidget = QWidget()
self.setLayout(categoryLayout)
def onCategoryAddClicked(self):
try:
if (self.cur is None):
raise Exception('Not connected to database')
def onCategoryAddClicked(self):
# 创建添加数据对话框
addDialog = QDialog(self)
@ -76,14 +79,29 @@ def onCategoryAddClicked(self):
data = {"name": line1.text(), "income_type": type, "description": description}
data = json.dumps(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:
# 处理插入数据错误
print(e)
QMessageBox.critical(self, 'Error', str(e))
except Exception as e:
# 处理获取表格列名错误
print(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:
print(e)
QMessageBox.critical(self, 'Error', str(e))

View file

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

View file

@ -1,84 +1,114 @@
from PyQt6.QtWidgets import *
import psycopg2
from ._tab import TabPage
def connectToDatabase(self):
try:
self.conn = psycopg2.connect(database=self.dbName, user=self.user, password=self.password, host=self.host, port=self.port)
self.cur = self.conn.cursor()
except Exception as e:
print(e)
self.cur = None
QMessageBox.critical(self, 'Error', str(e))
class PostgresTab(TabPage):
def __init__(self):
super().__init__()
self.initUI()
def disconnectFromDatabase(self):
try:
if (self.cur is None):
raise Exception('Not connected to database')
self.cur.close()
self.conn.close()
self.cur = None
except Exception as e:
print(e)
QMessageBox.critical(self, 'Error', str(e))
cur = None
conn = None
def connected(self):
return self.cur is not None
def onConnectClicked(self):
if self.connectButton.text() == 'Connect':
self.host = self.hostLineEdit.text()
self.port = self.portLineEdit.text()
self.dbName = self.dbNameLineEdit.text()
self.user = self.userLineEdit.text()
self.password = self.passwordLineEdit.text()
# 连接到 PostgreSQL 数据库
self.connectToDatabase()
self.connectButton.setText('Disconnect')
else:
self.disconnectFromDatabase()
self.connectButton.setText('Connect')
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):
try:
self.conn = psycopg2.connect(database=self.dbName, user=self.user, password=self.password, host=self.host, port=self.port)
self.cur = self.conn.cursor()
except Exception as e:
print(e)
self.cur = None
QMessageBox.critical(self, 'Error', str(e))
def disconnectFromDatabase(self):
try:
if (self.cur is None):
raise Exception('Not connected to database')
self.cur.close()
self.conn.close()
self.cur = None
except Exception as e:
print(e)
QMessageBox.critical(self, 'Error', str(e))
def initConnectTab(self):
# 创建连接信息控件
self.hostLabel = QLabel('Host:')
self.hostLineEdit = QLineEdit()
self.hostLineEdit.setText('159.75.75.169')
def onConnectClicked(self):
if self.connectButton.text() == 'Connect':
self.host = self.hostLineEdit.text()
self.port = self.portLineEdit.text()
self.dbName = self.dbNameLineEdit.text()
self.user = self.userLineEdit.text()
self.password = self.passwordLineEdit.text()
# 连接到 PostgreSQL 数据库
self.connectToDatabase()
self.connectButton.setText('Disconnect')
else:
self.disconnectFromDatabase()
self.connectButton.setText('Connect')
self.portLabel = QLabel('Port:')
self.portLineEdit = QLineEdit()
self.portLineEdit.setText('5432')
self.dbNameLabel = QLabel('Database:')
self.dbNameLineEdit = QLineEdit()
self.dbNameLineEdit.setText('budget')
def initUI(self):
# 创建连接信息控件
self.hostLabel = QLabel('Host:')
self.hostLineEdit = QLineEdit()
self.hostLineEdit.setText('159.75.75.169')
self.userLabel = QLabel('Username:')
self.userLineEdit = QLineEdit()
self.userLineEdit.setText('budget')
self.portLabel = QLabel('Port:')
self.portLineEdit = QLineEdit()
self.portLineEdit.setText('5432')
self.passwordLabel = QLabel('Password:')
self.passwordLineEdit = QLineEdit()
self.passwordLineEdit.setEchoMode(QLineEdit.EchoMode.Password)
self.passwordLineEdit.setText('budget')
self.dbNameLabel = QLabel('Database:')
self.dbNameLineEdit = QLineEdit()
self.dbNameLineEdit.setText('budget')
self.connectButton = QPushButton('Connect')
self.userLabel = QLabel('Username:')
self.userLineEdit = QLineEdit()
self.userLineEdit.setText('budget')
# 连接信号槽
self.connectButton.clicked.connect(self.onConnectClicked)
self.passwordLabel = QLabel('Password:')
self.passwordLineEdit = QLineEdit()
self.passwordLineEdit.setEchoMode(QLineEdit.EchoMode.Password)
self.passwordLineEdit.setText('budget')
# 创建连接信息控件布局
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)
self.connectButton = QPushButton('Connect')
connectWidget = QWidget()
connectWidget.setLayout(connectLayout)
return connectWidget
# 连接信号槽
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)
self.setLayout(connectLayout)

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