首页 > 其他分享 >PyQt6/PySide6:账本项目前端制作【附完整项目地址】

PyQt6/PySide6:账本项目前端制作【附完整项目地址】

时间:2024-09-08 23:14:13浏览次数:12  
标签:.__ data self PyQt6 filename PySide6 ui 账本 def

0. 前言

最近在家里闲着没事,正好又看到朋友@studentWheat发了篇用Tkinter做的账本,于是决定跟他一起改进这个程序。

屏幕截图:
MainWindow

dlgAdd stat

1. 后端

后端主要是朋友做的,在这里就不多说了,放个代码:
src/api.py

from collections import defaultdict

class ApiError(RuntimeError):
    pass

def openFile(filename):
    '''
    Open file.
    File format: 4 lines per record for date, event type, money delta, and note.
    Such as:
    (file.example, encoding=utf-8)
      (Record 1)
        (ln 1) date1
        (ln 2) event_type1
        (ln 3) money_delta1
        (ln 4) note1
      (Record 2)
        (ln 5) date2
        (ln 6) event_type2
        (ln 7) money_delta2
        (ln 8) note2
    @param filename: File name.
    Returns: data in the format [[date1, event_type1, money_delta1, note1], ...]
    '''
    with open(filename, 'r', encoding='utf-8') as f:
        res = []
        while date := f.readline():
            if (etype := f.readline()) and (mdelta := f.readline()) and (note := f.readline()):
                res.append([date.rstrip('\n'), etype.rstrip('\n'), mdelta.rstrip('\n'), note.rstrip('\n')])
            else:
                raise ApiError('Unexpected EOF at ' + filename)
        return res

def saveFile(filename, data): # Save
    '''
    Save with the same format mentioned in openFile().
    @param filename: File name.
    @param data: Data with the same format returned in openFile().
    '''
    with open(filename, 'w', encoding='utf-8') as f:
        for line in data:
            print(*line, sep='\n', file=f)

def query(data, key):
    return [record for record in data if any(key in x for x in record)] if key else data

def total(data):
    in_total = out_total = 0
    for _, _, mdelta, _ in data:
        mdelta = int(mdelta)
        if mdelta < 0:
            out_total -= mdelta
        else:
            in_total += mdelta
    return in_total, out_total

def totalByEvent(data):
    cnt = defaultdict(lambda: [0, 0])
    for _, event, mdelta, _ in data:
        mdelta = int(mdelta)
        if mdelta < 0:
            cnt[event][1] -= mdelta
        else:
            cnt[event][0] += mdelta
    return cnt

def totalByDate(data):
    cnt = defaultdict(lambda: [0, 0])
    for date, _, mdelta, _ in data:
        mdelta = int(mdelta)
        if mdelta < 0:
            cnt[date][1] -= mdelta
        else:
            cnt[date][0] += mdelta
    return cnt

详见https://blog.csdn.net/qq_67190987/article/details/125918530

2. 前端

正如标题中所说,框架采用Qt6+Python,一般有两种选择(PyQt6PySide6),我这里使用的是PySide6

2.1 准备资源

src/icons下存好所有图片资源:
图片

2.2 Designer 窗口绘制

用Qt Designer绘制好各个窗口,如图:

QMainWindow QDialog

2.3 安装依赖项

准备一份requirements.txt,内容如下:

PySide6>=6.3.1

然后,cmd中输入:

pip install -r requirements.txt

搞定安装。

2.4 编译资源和UI

这个就不用多说了,直接用pyside6-uicpyside6-rcc命令编译文件,编译出的文件列表如下:

AccountBook
└─src
    │  dlgAdd.ui
    │  dlgCharts.ui
    │  MainWindow.ui
    │  res.qrc
    │  res_rc.py
    │  ui_dlgAdd.py
    │  ui_dlgCharts.py
    │  ui_dlgHelp.py
    │  ui_MainWindow.py

2.5 代码编写

src/dlgAdd.py
“添加账目”窗口,一个简单的QDialog实例。

from PySide6.QtWidgets import *
from PySide6.QtCore import QDate, QRegularExpression
from PySide6.QtGui import QRegularExpressionValidator
from ui_dlgAdd import Ui_Dialog

class dlgAdd(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.dateEdit.setDate(QDate.currentDate())
        self.ui.moneyEdit.setValidator(QRegularExpressionValidator(QRegularExpression(r'(\+|\-)[1-9]+[0-9]*')))
        self.ui.buttonBox.button(QDialogButtonBox.Ok).setText('确定')
        self.ui.buttonBox.button(QDialogButtonBox.Cancel).setText('取消')
    
    def getRow(self):
        date = self.ui.dateEdit.text()
        event = self.ui.eventEdit.text()
        money = self.ui.moneyEdit.text()
        note = self.ui.noteEdit.text()
        return [date, event, money, note]

    def accept(self):
        if not self.ui.eventEdit.text():
            QMessageBox.critical(self, "错误", "事件不能为空,请重新填写。")
            return
        if self.ui.moneyEdit.text() in ('', '+', '-'):
            QMessageBox.critical(self, "错误", "金额不能为空,请重新填写。")
            return
        return super().accept()

src/dlgCharts.py
图表展示窗口,使用QtCharts(用法跟PyQt5/PySide2略有区别)绘制柱状图。后续会考虑增加更多图表。

from bisect import bisect_left, bisect_right

from PySide6.QtCore import QDate, Qt
from PySide6.QtWidgets import *
from PySide6.QtCharts import QBarCategoryAxis, QBarSeries, QBarSet, QChart, QChartView, QValueAxis

from api import total, totalByDate, totalByEvent
from ui_dlgCharts import Ui_Dialog

class dlgCharts(QDialog):
    def __init__(self, data, parent=None):
        super().__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.showMaximized()

        self.data = data
        minDate = QDate.fromString(data[0][0], 'yyyy/MM/dd')
        maxDate = QDate.fromString(data[-1][0], 'yyyy/MM/dd')
        self.ui.startDateEdit.setDateRange(minDate, maxDate)
        self.ui.endDateEdit.setDateRange(minDate, maxDate)
        self.ui.startDateEdit.setDate(minDate)
        self.ui.endDateEdit.setDate(maxDate)

        self.__update_totalChart(*total(data))
        self.__update_eventChart(totalByEvent(data))
        self.__update_dateChart(totalByDate(data))

        self.ui.startDateEdit.editingFinished.connect(self.__updateCharts)
        self.ui.endDateEdit.editingFinished.connect(self.__updateCharts)

    @staticmethod
    def createChart(chartView: QChartView, title, xAxis, yAxisList):
        chart = QChart()
        chart.setTitle(title)
        chart.setAnimationOptions(QChart.SeriesAnimations)

        series = QBarSeries()
        for axisName, data in yAxisList:
            barSet = QBarSet(axisName)
            barSet.append(data)
            series.append(barSet)
        chart.addSeries(series)

        axisX = QBarCategoryAxis()
        axisX.append(xAxis)
        chart.addAxis(axisX, Qt.AlignBottom)
        series.attachAxis(axisX)

        axisY = QValueAxis()
        axisY.setLabelFormat('%d')
        chart.addAxis(axisY, Qt.AlignLeft)
        series.attachAxis(axisY)

        chartView.setChart(chart)

    def __update_totalChart(self, total_in, total_out):
        self.createChart(
            chartView = self.ui.totalView,
            title     = '总收支',
            xAxis     = ['收入', '支出'], 
            yAxisList = [
                ('金额', [total_in, total_out])
            ]
        )

    def __update_eventChart(self, events):
        self.createChart(
            chartView = self.ui.eventView,
            title     = '收支分类',
            xAxis     = list(events.keys()),
            yAxisList = [
                ('收入', list(map(lambda x: x[0], events.values()))),
                ('支出', list(map(lambda x: x[1], events.values())))
            ]
        )

    def __update_dateChart(self, dates):
        self.createChart(
            chartView = self.ui.dateView,
            title     = '每日收支',
            xAxis     = list(dates.keys()),
            yAxisList = [
                ('收入', list(map(lambda x: x[0], dates.values()))),
                ('支出', list(map(lambda x: x[1], dates.values())))
            ]
        )

    def __updateCharts(self):
        startDate = self.ui.startDateEdit.text()
        endDate = self.ui.endDateEdit.text()
        left = bisect_left(self.data, startDate, key=lambda x: x[0])
        right = bisect_right(self.data, endDate, key=lambda x: x[0])
        data = self.data[left:right]
        self.__update_totalChart(*total(data))
        self.__update_eventChart(totalByEvent(data))
        self.__update_dateChart(totalByDate(data))

src/main.py
主程序,同时管理主窗口。最麻烦的地方是QTableView,要同时处理搜索和排序问题。

import sys
from bisect import insort_right
from functools import partial
from os.path import basename
from webbrowser import open_new_tab

from PySide6.QtWidgets import *
from PySide6.QtCore import Slot, QDate
from PySide6.QtGui import QStandardItem, QStandardItemModel

from api import ApiError, openFile, query, saveFile
from dlgAdd import dlgAdd
from dlgCharts import dlgCharts
from ui_dlgHelp import Ui_Dialog as Ui_dlgHelp
from ui_MainWindow import Ui_MainWindow

# Version info
VERSION = '1.0.1'
CHANNEL = 'stable'
BUILD_DATE = '2022-07-01'
FULL_VERSION = f'{VERSION}-{CHANNEL} ({BUILD_DATE}) on {sys.platform}'

app = QApplication(sys.argv)

class AccountBookMainWindow(QMainWindow):
    version_str = '账本 ' + VERSION
    unsaved_tip = '*'
    SUPPORTED_FILTERS = '账本文件(*.abf);;文本文件(*.txt);;所有文件(*.*)'

    def __init__(self, parent=None):
        # Initialize window
        super().__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setWindowTitle('账本 ' + VERSION)
        self.labStatus = QLabel(self)
        self.ui.statusBar.addWidget(self.labStatus)

        # Initialize table
        self.model = QStandardItemModel(0, 4, self)
        self.model.setHorizontalHeaderLabels(['日期', '事项', '金额', '备注'])
        self.ui.table.setModel(self.model)
        self.ui.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

        self.__data = []
        self.on_actFile_New_triggered()
        self.ui.actEdit_Remove.setEnabled(False)

        # Connect slots
        self.ui.table.selectionModel().selectionChanged.connect(self.__selectionChanged)
        self.model.itemChanged.connect(self.__itemChanged)

    def __updateTable(self, data):
        self.model.itemChanged.disconnect(self.__itemChanged)
        self.model.setRowCount(len(data))
        for row in range(len(data)):
            for col in range(len(data[row])):
                self.model.setItem(row, col, QStandardItem(data[row][col]))
        self.model.itemChanged.connect(self.__itemChanged)

    def __openFile(self, filename):
        try:
            self.__data = openFile(filename)
        except IOError:
            QMessageBox.critical(self, '错误', '文件打开失败。请稍后再试。')
        except ApiError:
            QMessageBox.critical(self, '错误', '文件格式错误。请检查文件完整性。')
        except Exception as e:
            QMessageBox.critical(self, '错误', '未知错误:' + str(e.with_traceback()))
        else:
            self.ui.searchEdit.clear()
            self.__key = ''
            self.__updateTable(self.__data)
            self.labStatus.setText(filename)
            self.setWindowTitle(self.version_str)
            self.__filename = filename

    def __saveFile(self, filename):
        try:
            saveFile(filename, self.__data)
        except IOError:
            QMessageBox.critical(self, '错误', '文件保存错误。请稍后再试。')
        except Exception as e:
            QMessageBox.critical(self, '错误', '未知错误:' + str(e.with_traceback()))
        else:
            self.labStatus.setText('保存成功:' + filename)
            self.setWindowTitle(self.version_str)
            self.__filename = filename

    @Slot()
    def on_actFile_New_triggered(self):
        self.__filename = self.__key = ''
        self.setWindowTitle(self.unsaved_tip + self.version_str)
        self.labStatus.setText('新文件')
        self.model.setRowCount(0)
        self.__data.clear()

    @Slot()
    def on_actFile_Open_triggered(self):
        filename, _ = QFileDialog.getOpenFileName(self, '打开', filter=self.SUPPORTED_FILTERS)
        if filename:
            self.__openFile(filename)

    @Slot()
    def on_actFile_Save_triggered(self):
        if self.__filename:
            self.__saveFile(self.__filename)
        else:
            filename, _ = QFileDialog.getSaveFileName(self, '保存', filter=self.SUPPORTED_FILTERS)
            if filename:
                self.__saveFile(filename)

    @Slot()
    def on_actFile_SaveAs_triggered(self):
        filename, _ = QFileDialog.getSaveFileName(self, '另存为', filter=self.SUPPORTED_FILTERS)
        if filename:
            self.__saveFile(filename)

    @Slot()
    def on_actHelp_About_triggered(self):
        dialog = QDialog(self)
        ui = Ui_dlgHelp()
        ui.setupUi(dialog)
        for link in (ui.githubLink, ui.giteeLink, ui.licenseLink, ui.readmeLink):
            link.clicked.connect(partial(open_new_tab, link.description()))
        ui.labVersion.setText('版本号:' + FULL_VERSION)
        ui.btnUpdate.clicked.connect(partial(open_new_tab, 'https://github.com/GoodCoder666/AccountBook/releases'))
        dialog.exec()

    @Slot()
    def on_actHelp_AboutQt_triggered(self):
        QMessageBox.aboutQt(self, '关于Qt')

    @Slot()
    def on_actEdit_Add_triggered(self):
        dialog = dlgAdd(self)
        if dialog.exec() == QDialog.Accepted:
            row = dialog.getRow()
            insort_right(self.__data, row)
            self.__updateTable(query(self.__data, self.__key))
            self.setWindowTitle(self.unsaved_tip + self.version_str)

    @Slot()
    def on_actEdit_Remove_triggered(self):
        rows = list(set(map(lambda idx: idx.row(), self.ui.table.selectedIndexes())))
        for row in rows:
            self.__data.remove([self.model.item(row, col).text() for col in range(self.model.columnCount())])
        self.model.itemChanged.disconnect(self.__itemChanged)
        self.model.removeRows(rows[0], len(rows))
        self.model.itemChanged.connect(self.__itemChanged)
        self.setWindowTitle(self.unsaved_tip + self.version_str)

    def __selectionChanged(self):
        self.ui.actEdit_Remove.setEnabled(self.ui.table.selectionModel().hasSelection())

    def __itemChanged(self, item: QStandardItem):
        i, j, new = item.row(), item.column(), item.text()
        if (old := self.__data[i][j]) == new: return
        if j == 0 and not QDate.fromString(new, 'yyyy/MM/dd').isValid():
            QMessageBox.critical(self, '错误', '日期格式错误。')
            self.model.itemChanged.disconnect(self.__itemChanged)
            item.setText(old)
            self.model.itemChanged.connect(self.__itemChanged)
            return
        row = self.__data.pop(i)
        row[j] = new
        insort_right(self.__data, row)
        self.__updateTable(query(self.__data, self.__key))
        self.setWindowTitle(self.unsaved_tip + self.version_str)

    @Slot()
    def on_searchEdit_textChanged(self):
        self.__key = self.ui.searchEdit.text()
        self.__updateTable(query(self.__data, self.__key))

    @Slot()
    def on_actStat_Show_triggered(self):
        if self.__data:
            dlgCharts(self.__data, self).exec()
        else:
            QMessageBox.information(self, '提示', '请添加数据以使用统计功能。')

    def closeEvent(self, event):
        if not self.windowTitle().startswith(self.unsaved_tip): return
        filename = basename(self.__filename) if self.__filename else '新文件'
        messageBox = QMessageBox(
            parent=self, icon=QMessageBox.Warning, windowTitle='提示',
            text=f'是否要保存对 {filename} 的更改?', informativeText='如果不保存,你的更改将丢失。',
            standardButtons=QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel
        )
        messageBox.setButtonText(QMessageBox.Save, '保存')
        messageBox.setButtonText(QMessageBox.Discard, '不保存')
        messageBox.setButtonText(QMessageBox.Cancel, '取消')
        reply = messageBox.exec()
        if reply == QMessageBox.Save:
            self.on_actFile_Save_triggered()
            event.accept()
        elif reply == QMessageBox.Discard:
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        self.__openFile(event.mimeData().text()[8:]) # [8:] is to get rid of 'file:///'

mainform = AccountBookMainWindow()
mainform.show()

sys.exit(app.exec())

3. 总结

本项目到此结束。

【附:项目地址】

记得点个Star哦~


创作不易,若您喜欢这篇文章就请点个三连吧!万分感激!!!

标签:.__,data,self,PyQt6,filename,PySide6,ui,账本,def
From: https://www.cnblogs.com/stanleys/p/18403690/pyside6-accountbook

相关文章

  • Python教程(二十一) : 从零开始制作计算器应用【PyQt6】
    文章目录专栏列表环境准备代码解析主要组件初始化界面布局设置事件处理计算逻辑运行应用完整代码示例截图总结注意专栏列表Python教程(十):面向对象编程(OOP)Python教程(十一):单元测试与异常捕获Python教程(十二):面向对象高级编程详解Python教程(十三):常用内置模块详解Python......
  • 记录 PyQt6 / PySide 6 自定义边框窗口的 Bug 及可能可行的解决方案:窗口抖动和添加 DW
    前言:本篇文章将要讨论我在前不久发表的关于PyQt6/PySide6自定义边框窗口代码及内容中的问题:(终)PyQt6/PySide6+Pywin32自定义标题栏窗口+完全还原Windows原生窗口边框特效_pyside6win32无边框窗口-CSDN博客https://blog.csdn.net/2402_84665876/article/detail......
  • 基于PySide6的串口组手
    目录一、软件界面二、各部分详情1、串口设置2、显示设置3、发送区设置4、接收区设置5、各部分统一一、软件界面如图所示,主要由串口设置、显示设置、发送区、接收区四部分组成:二、各部分详情1、串口设置设置串口、波特率、数据位、停止位、数据位、停止位、校......
  • 基于小程序个人收支理财记账本小程序微信小程序项目(源码+文档+运行视频+讲解视频)
    前言......
  • Python使用PyCharm+PySide6+Pandas创建QTableView显示Excel工作簿数据
    importsysimportwarningsfrompathlibimportPathimportpandasaspdfromPySide6importQtWidgetsfromPySide6.QtCoreimportQtfromPySide6.QtGuiimportQStandardItemModel#读取Excel工作簿数据defread_excel_with_pandas(path_excel,_cols):"&qu......
  • 如何在 PyQt6 中调整窗口大小期间将标题和图像粘合在一起
    我正在开发一个GUI,其中有一个QMainWindow,其布局包含三个垂直堆叠的小部件:顶部标题、图像和底部标题。顶部和底部标题是带有蓝色背景的自定义QWidget,图像使用带有QPixmap的QLabel显示。该图像只是一个通用的400x400图像。这是我的代码:fromPyQt6.QtWidgetsimport......
  • Qt-pyqt6与QTDesginers的相互使用技巧
    1.先在QTDesignersTools设计器中画好框架,再保存为.ui文件导出2.再pycharm中创建一个main.py文件用来加载和使用这个.ui文件,通用的代码如下:main.pyfromPyQt6.QtGuiimportQIntValidator,QIconfromPyQt6.QtWidgetsimportQApplication,QLabel,QListWidgetItemfrom......
  • 入门 PyQt6 看过来(基础)23~ 定时器和线程
    一般情况下,应用程序都是单线程运行的,但如果需要执行一个特别耗时的操作,GUI界面其他操作就无法进行,用户感觉程序没有了响应,或者windows系统也认为程序运行出现了问题并自动关闭了程序。有类似的情况应用就需要采用定时器和线程。1定时器(QTimer)如果要在应用程序中周期性地运......
  • 5、Qt-pyqt6常用基本控件 - 按钮控件
    Buttons--按钮类控件名说明PushButton按钮ToolButton工具按钮RadioButton单选按钮CheckBox复选框CommandLinkButton命令链接按钮DialogButtonBox对话框按钮盒......
  • 4、Qt-pyqt6常用基本控件
    控件对应QTDesigner中的左侧控件Layouts--布局管理控件名说明VerticalLayout垂直布局HorizontalLayout水平布局GridLayout网格布局FormLayout表单布局Spacers--弹簧控件名说明HoriziontalSpacer水平弹簧VerticalSpacer垂......