feat: more classes
change pg and category to class
This commit is contained in:
parent
b12807031a
commit
ce1c8c6df8
5 changed files with 171 additions and 106 deletions
|
@ -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
|
|
@ -1,7 +1,16 @@
|
|||
from PyQt6.QtWidgets import *
|
||||
from ._tab import TabPage
|
||||
from ._pg import PostgresTab
|
||||
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()
|
||||
|
||||
|
@ -13,18 +22,12 @@ def initCategorytTab(self):
|
|||
categoryLayout.addWidget(self.categoryComboBox)
|
||||
categoryLayout.addWidget(self.categoryAddButton)
|
||||
|
||||
# 连接信号槽
|
||||
# self.connectButton.clicked.connect(self.onConnectClicked)
|
||||
|
||||
categoryWidget = QWidget()
|
||||
categoryWidget.setLayout(categoryLayout)
|
||||
return categoryWidget
|
||||
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))
|
||||
|
||||
|
||||
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))
|
|
@ -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()
|
||||
|
|
|
@ -1,7 +1,39 @@
|
|||
from PyQt6.QtWidgets import *
|
||||
import psycopg2
|
||||
from ._tab import TabPage
|
||||
|
||||
def connectToDatabase(self):
|
||||
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):
|
||||
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()
|
||||
|
@ -10,7 +42,7 @@ def connectToDatabase(self):
|
|||
self.cur = None
|
||||
QMessageBox.critical(self, 'Error', str(e))
|
||||
|
||||
def disconnectFromDatabase(self):
|
||||
def disconnectFromDatabase(self):
|
||||
try:
|
||||
if (self.cur is None):
|
||||
raise Exception('Not connected to database')
|
||||
|
@ -22,7 +54,7 @@ def disconnectFromDatabase(self):
|
|||
QMessageBox.critical(self, 'Error', str(e))
|
||||
|
||||
|
||||
def onConnectClicked(self):
|
||||
def onConnectClicked(self):
|
||||
if self.connectButton.text() == 'Connect':
|
||||
self.host = self.hostLineEdit.text()
|
||||
self.port = self.portLineEdit.text()
|
||||
|
@ -37,7 +69,7 @@ def onConnectClicked(self):
|
|||
self.connectButton.setText('Connect')
|
||||
|
||||
|
||||
def initConnectTab(self):
|
||||
def initUI(self):
|
||||
# 创建连接信息控件
|
||||
self.hostLabel = QLabel('Host:')
|
||||
self.hostLineEdit = QLineEdit()
|
||||
|
@ -79,6 +111,4 @@ def initConnectTab(self):
|
|||
connectLayout.addWidget(self.passwordLineEdit)
|
||||
connectLayout.addWidget(self.connectButton)
|
||||
|
||||
connectWidget = QWidget()
|
||||
connectWidget.setLayout(connectLayout)
|
||||
return connectWidget
|
||||
self.setLayout(connectLayout)
|
10
src/budget/_tab.py
Normal file
10
src/budget/_tab.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
from PyQt6.QtWidgets import *
|
||||
import psycopg2
|
||||
|
||||
class TabPage(QWidget):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def selected(self):
|
||||
pass
|
Loading…
Reference in a new issue