收支记录程序 最后更新时间:2021年03月08日 看着自己日渐消瘦的钱包,我于心不忍。为什么会这样呢?我陷入思考,忽然悟到,我对于平时的收支根本不甚清楚。即使有可以去记,也有可能只是估算个大概。这对于日常收支来说太不可靠了。为了能帮助自己有效的计算每日收支,所以就开发了这一款程序来辅助自己。 程序并不会进行计算,只是做出辅助。之所以不直接使用数据库。是因为数据库查看相对显得麻烦,且对于简单的日常收支辅助来说,每次都去打开数据库,显得没有必要。 ### 设计思路: - 创建主窗口 - 数据库数据遍历显示在主窗口 - 创建添加按钮及窗口 - 创建修改按钮及窗口 - 创建登陆程序 ### 重点及难点: - 数据库数据的显示 - 子窗口的弹出 - 数据的添加和修改 ### 解决方法: - 数据库数据的显示 采用遍历的方法,统计记录条数,遍历产生出显示数据的输入框,输入框只读。 - 子窗口的弹出 核心问题是,子窗口弹出第一次时正常,第二次弹出就会阻塞程序。解决方法是,不要将表示子窗口的方法名和属性名相同。 - 数据的添加和修改 数据的添加方面,由于有时间问题,如果不添加时间,就默认显示当前系统时间。但由于判断时如同没有添加时间,就会阻塞程序。所以在每个时间文本后面加个空格,如果结果为空格,就不添加时间。如果不为空格,则用rstrip方法删除字符末尾空格,然后添加数据。数据的修改同理,由于不确定到底哪些数据修改,哪些不修改,则根据数据内容是否为空格做判断。 ### 注意: - 这里添加登陆程序,主要是有一个基本的安全作用,并没有和数据库之类的连接,数据库部分启用单独的密码和账号。 ### 实际代码: 主程序: ```python # coding:utf-8 from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * from DPT import DPT import datetime import sys dpt = DPT() id = dpt.id() datatime =dpt.datatime() income = dpt.income() spend = dpt.spending() sd_cause = dpt.sd_cause() now_balance = dpt.now_balance() balance = dpt.balance() class App(QWidget): def __init__(self): super().__init__() # self.win_main() def win_main(self): self.resize(1080,720) self.setFixedSize(self.width(),self.height()) self.setWindowFlags(Qt.WindowCloseButtonHint) self.setWindowIcon(QIcon('img/logo.ico')) self.setWindowTitle("收支记录器") self.bg =QLabel(self) self.bg.resize(600,720) self.bg.move(400,0) bg = QPixmap('img/bg.png') self.bg.setPixmap(bg) self.lab = QLabel(self) self.lab.resize(48,48) self.lab.move(175,15) he = QPixmap('img/黑色骑士团.png') self.lab.setPixmap(he) self.lab = QLabel(self) self.lab.resize(48, 48) self.lab.move(365, 15) he = QPixmap('img/黑色骑士团.png') self.lab.setPixmap(he) self.lab = QLabel(self) self.lab.resize(48, 48) self.lab.move(485, 15) he = QPixmap('img/黑色骑士团.png') self.lab.setPixmap(he) self.lab = QLabel(self) self.lab.resize(48, 48) self.lab.move(595, 15) he = QPixmap('img/黑色骑士团.png') self.lab.setPixmap(he) self.lab = QLabel(self) self.lab.resize(48, 48) self.lab.move(710, 15) he = QPixmap('img/黑色骑士团.png') self.lab.setPixmap(he) self.lab = QLabel(self) self.lab.resize(48, 48) self.lab.move(835, 15) he = QPixmap('img/黑色骑士团.png') self.lab.setPixmap(he) self.label = QLabel("时间", self) self.label.move(230, 30) self.label = QLabel("收入", self) self.label.move(420, 30) self.label = QLabel("支出", self) self.label.move(540, 30) self.label = QLabel("支出原因", self) self.label.move(650, 30) self.label = QLabel("今日余额", self) self.label.move(765, 30) self.label = QLabel("总余额", self) self.label.move(890, 30) self.frame = QFrame (self) self.frame.resize(980, 540) self.frame.move(50, 60) self.frame.setStyleSheet("QFrame{border: 1px solid;}") self.scroll = QScrollBar(self.frame) self.scroll.resize(10,538) self.scroll.move(969,1) self.scroll.setMaximum(1000) self.scroll.sliderMoved.connect(self.gun) self.frame_2 = QFrame(self.frame) self.frame_2.resize(968,2000) self.frame_2.move(0,0) self.frame_2.setStyleSheet("QFrame{border: 1px solid;}") for num in range(0,len(id)): """图标""" self.tb = QLabel(self.frame_2) self.tb.resize(48, 48) self.tb.move(35, 15+num*30) he2 = QPixmap('img/黑色骑士团-2.png') self.tb.setPixmap(he2) self.tb.setStyleSheet("QLabel{border: 0px;}") """ID""" locals()["self.id" + str(num)] = QLineEdit(self.frame_2) locals()["self.id" + str(num)].setPlaceholderText(str(num+1)) locals()["self.id" + str(num)].setReadOnly(True) locals()["self.id" + str(num)].resize(20,20) locals()["self.id" + str(num)].move(60,30+num*30) """日期时间""" locals()["self.time" + str(num)] = QLineEdit(self.frame_2) locals()["self.time" + str(num)].setPlaceholderText(str(datatime[num])) locals()["self.time" + str(num)].setReadOnly(True) locals()["self.time" + str(num)].resize(220, 20) locals()["self.time" + str(num)].move(100, 30 + num * 30) """收入""" locals()["self.income" + str(num)] = QLineEdit(self.frame_2) if income[num] != None: locals()["self.income" + str(num)].setPlaceholderText(str(income[num])) else: locals()["self.income" + str(num)].setPlaceholderText(" ") locals()["self.income" + str(num)].setReadOnly(True) locals()["self.income" + str(num)].resize(100, 20) locals()["self.income" + str(num)].move(340, 30 + num * 30) """支出""" locals()["self.spending" + str(num)] = QLineEdit(self.frame_2) if spend[num] != None: locals()["self.spending" + str(num)].setPlaceholderText(str(spend[num])) else: locals()["self.spending" + str(num)].setPlaceholderText(" ") locals()["self.spending" + str(num)].setReadOnly(True) locals()["self.spending" + str(num)].resize(100, 20) locals()["self.spending" + str(num)].move(460, 30 + num * 30) """支出原因""" locals()["self.sd_cause" + str(num)] = QLineEdit(self.frame_2) if sd_cause != None: locals()["self.sd_cause" + str(num)].setPlaceholderText(str(sd_cause[num])) else: locals()["self.sd_cause" + str(num)].setPlaceholderText(" ") locals()["self.sd_cause" + str(num)].setReadOnly(True) locals()["self.sd_cause" + str(num)].resize(100, 20) locals()["self.sd_cause" + str(num)].move(580, 30 + num * 30) """今日余额""" locals()["self.now_balance" + str(num)] = QLineEdit(self.frame_2) locals()["self.now_balance" + str(num)].setPlaceholderText(str(now_balance[num])) locals()["self.now_balance" + str(num)].setReadOnly(True) locals()["self.now_balance" + str(num)].resize(100, 20) locals()["self.now_balance" + str(num)].move(700, 30 + num * 30) """总余额""" locals()["self.balance" + str(num)] = QLineEdit(self.frame_2) locals()["self.balance" + str(num)].setPlaceholderText(str(balance[num])) locals()["self.balance" + str(num)].setReadOnly(True) locals()["self.balance" + str(num)].resize(100, 20) locals()["self.balance" + str(num)].move(820,30 + num * 30) self.frame_3 = QFrame(self) self.frame_3.resize(980,80) self.frame_3.move(50,620) self.frame_3.setStyleSheet("QFrame{border: 1px solid;}") self.add_button=QPushButton("添加", self.frame_3) self.add_button.resize(200,60) self.add_button.move(150,10) self.add_button.setStyleSheet("QPushButton{background:#000000;color:#ffffff;font-size:24px;}QPushButton::hover{background:#777777;color:#ffffff;font-size:24px;}") self.add_button.clicked.connect(lambda: self.win_add()) self.fix_button = QPushButton("修改", self.frame_3) self.fix_button.resize(200,60) self.fix_button.move(630, 10) self.fix_button.setStyleSheet("QPushButton{background:#000000;color:#ffffff;font-size:24px;}QPushButton::hover{background:#777777;color:#ffffff;font-size:24px;}") self.fix_button.clicked.connect(lambda: self.win_fix()) self.show() def win_add(self): self.window_add=QWidget() self.window_add.resize(300,500) self.window_add.setFixedSize(self.window_add.width(), self.window_add.height()) self.window_add.setWindowFlags(Qt.WindowCloseButtonHint) self.window_add.setWindowTitle("添加数据") self.window_add.setWindowIcon(QIcon('img/logo.ico')) self.frame_4 = QFrame(self.window_add) self.frame_4.resize(280,440) self.frame_4.move(10,10) self.frame_4.setStyleSheet("QFrame{border: 1px solid;}") self.nowtime_label=QLabel("时间:",self.frame_4) self.nowtime_label.resize(60,30) self.nowtime_label.move(20,10) self.nowtime_label.setStyleSheet("QLabel{border: 0px;}") self.nowtime_edit =QLineEdit(self.frame_4) self.nowtime_edit.resize(160,30) self.nowtime_edit.move(80,10) self.incame_label=QLabel("收入:",self.frame_4) self.incame_label.resize(60,30) self.incame_label.move(20,60) self.incame_label.setStyleSheet("QLabel{border: 0px;}") self.incame_edit = QLineEdit(self.frame_4) self.incame_edit.resize(160, 30) self.incame_edit.move(80, 60) self.spend_label = QLabel("支出:", self.frame_4) self.spend_label.resize(60, 30) self.spend_label.move(20, 110) self.spend_label.setStyleSheet("QLabel{border: 0px;}") self.spend_edit = QLineEdit(self.frame_4) self.spend_edit.resize(160, 30) self.spend_edit.move(80, 110) self.sd_label = QLabel("支出原因:", self.frame_4) self.sd_label.resize(80, 30) self.sd_label.move(5, 160) self.sd_label.setStyleSheet("QLabel{border: 0px;}") self.sd_edit = QLineEdit(self.frame_4) self.sd_edit.resize(160, 30) self.sd_edit.move(80, 160) self.nb_label = QLabel("今日余额:", self.frame_4) self.nb_label.resize(80, 30) self.nb_label.move(5, 210) self.nb_label.setStyleSheet("QLabel{border: 0px;}") self.nb_edit = QLineEdit(self.frame_4) self.nb_edit.resize(160, 30) self.nb_edit.move(80, 210) self.balance_label = QLabel("总余额:", self.frame_4) self.balance_label.resize(60, 30) self.balance_label.move(10, 260) self.balance_label.setStyleSheet("QLabel{border: 0px;}") self.balance_edit = QLineEdit(self.frame_4) self.balance_edit.resize(160, 30) self.balance_edit.move(80, 260) self.add_sub = QPushButton("提交",self.window_add) self.add_sub.resize(80,30) self.add_sub.move(110,460) self.add_sub.setStyleSheet("QPushButton{background:#000000;color:#ffffff;font-size:24px;}QPushButton::hover{background:#777777;color:#ffffff;font-size:24px;}") self.bt_label = QLabel("",self.frame_4) self.bt_label.resize(125, 125) self.bt_label.move(90, 300) png = QPixmap('img/夜斗.png') self.bt_label.setPixmap(png) self.add_sub.clicked.connect(lambda: self.add()) self.window_add.show() def win_fix(self): self.window_fix = QWidget() self.window_fix.resize(300, 500) self.window_fix.setFixedSize(self.window_fix.width(), self.window_fix.height()) self.window_fix.setWindowFlags(Qt.WindowCloseButtonHint) self.window_fix.setWindowTitle("修改记录") self.window_fix.setWindowIcon(QIcon('img/logo.ico')) self.frame_5 = QFrame(self.window_fix) self.frame_5.resize(280, 440) self.frame_5.move(10, 10) self.frame_5.setStyleSheet("QFrame{border: 1px solid;}") self.id_label = QLabel("id:",self.frame_5) self.id_label.resize(60,30) self.id_label.move(20,10) self.id_label.setStyleSheet("QLabel{border: 0px;}") self.id_edit = QLineEdit(self.frame_5) self.id_edit.resize(160,30) self.id_edit.move(80,10) self.nowtime_label_fix = QLabel("时间:", self.frame_5) self.nowtime_label_fix.resize(60, 30) self.nowtime_label_fix.move(20, 60) self.nowtime_label_fix.setStyleSheet("QLabel{border: 0px;}") self.nowtime_edit_fix = QLineEdit(self.frame_5) self.nowtime_edit_fix.resize(160, 30) self.nowtime_edit_fix.move(80, 60) self.incame_label_fix = QLabel("收入:", self.frame_5) self.incame_label_fix.resize(60, 30) self.incame_label_fix.move(20, 110) self.incame_label_fix.setStyleSheet("QLabel{border: 0px;}") self.incame_edit_fix = QLineEdit(self.frame_5) self.incame_edit_fix.resize(160, 30) self.incame_edit_fix.move(80, 110) self.spend_label_fix = QLabel("支出:", self.frame_5) self.spend_label_fix.resize(60, 30) self.spend_label_fix.move(20, 160) self.spend_label_fix.setStyleSheet("QLabel{border: 0px;}") self.spend_edit_fix = QLineEdit(self.frame_5) self.spend_edit_fix.resize(160, 30) self.spend_edit_fix.move(80, 160) self.sd_label_fix = QLabel("支出原因:", self.frame_5) self.sd_label_fix.resize(80, 30) self.sd_label_fix.move(5, 210) self.sd_label_fix.setStyleSheet("QLabel{border: 0px;}") self.sd_edit_fix = QLineEdit(self.frame_5) self.sd_edit_fix.resize(160, 30) self.sd_edit_fix.move(80, 210) self.nb_label_fix = QLabel("今日余额:", self.frame_5) self.nb_label_fix.resize(80, 30) self.nb_label_fix.move(5, 260) self.nb_label_fix.setStyleSheet("QLabel{border: 0px;}") self.nb_edit_fix = QLineEdit(self.frame_5) self.nb_edit_fix.resize(160, 30) self.nb_edit_fix.move(80, 260) self.balance_label_fix = QLabel("总余额:", self.frame_5) self.balance_label_fix.resize(60, 30) self.balance_label_fix.move(10, 310) self.balance_label_fix.setStyleSheet("QLabel{border: 0px;}") self.balance_edit_fix = QLineEdit(self.frame_5) self.balance_edit_fix.resize(160, 30) self.balance_edit_fix.move(80, 310) self.fix_sub = QPushButton("提交",self.window_fix) self.fix_sub.resize(80, 30) self.fix_sub.move(110, 460) self.fix_sub.setStyleSheet("QPushButton{background:#000000;color:#ffffff;font-size:24px;}QPushButton::hover{background:#777777;color:#ffffff;font-size:24px;}") self.fix_sub.clicked.connect(lambda: self.fix()) self.window_fix.show() def gun(self): self.frame_2.move(0,-self.scroll.value()) def add(self): time = str(self.nowtime_edit.text())+" " incame = str(self.incame_edit.text()) spend = str(self.spend_edit.text()) sd_cause = self.sd_edit.text() n_balance = self.nb_edit.text() balance = self.balance_edit.text() dpt = DPT() dpt.conne() if time == " ": dpt.cursor.execute(f"INSERT INTO money(收入,支出,支出原因,今日余额,总余额) VALUES ('{incame}', '{spend}', '{sd_cause}', {n_balance}, {balance});") else: transit = [] for t in time: transit.append(t) transit.pop() time = str() for t in transit: time += t dpt.cursor.execute(f"INSERT INTO money(日期时间,收入,支出,支出原因,今日余额,总余额) VALUES ('{time}', '{incame}', '{spend}', '{sd_cause}', {n_balance}, {balance});") def fix(self): id = str(self.id_edit.text())+" " time = str(self.nowtime_edit_fix.text())+" " incame = str(self.incame_edit_fix.text())+" " spend = str(self.spend_edit_fix.text())+" " sd_cause = self.sd_edit_fix.text()+" " n_balance = self.nb_edit_fix.text()+" " balance = self.balance_edit_fix.text()+" " dpt = DPT() dpt.conne() print("1") if id == " ": print("错误,请输入id值") else: id.rstrip() if time != " ": time.rstrip() dpt.cursor.execute(f"update money set 日期时间='{time}' where id = '{id}'") if incame != " ": incame.rstrip() dpt.cursor.execute(f"update money set 收入 = '{incame}' where id = '{id}'") if spend != " ": spend.rstrip() dpt.cursor.execute(f"update money set 支出 = '{spend}' where id = '{id}'") if sd_cause != " ": sd_cause.rstrip() dpt.cursor.execute(f"update money set 支出原因 = '{sd_cause}' where id = '{id}'") if n_balance != " ": n_balance.rstrip() dpt.cursor.execute(f"update money set 今日余额 = '{n_balance}' where id = '{id}'") if balance != " ": balance.rstrip() dpt.cursor.execute(f"update money set 总余额 = '{balance}' where id = '{id}'") print("2") # dpt.cursor.execute(f"update money set 日期时间 = '{time}',收入='{incame}',支出='{spend}',支出原因='{sd_cause}',今日余额='{n_balance}',总余额='{balance}' where id = '{id}'") ``` 登陆程序: ```python #coding:utf-8 from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * from main import * from DPT import DPT import sys class Window_login(QDialog): def __init__(self): super().__init__() self.window_login(lr) """登陆界面""" def window_login(self,lr): self.resize(300,300) self.setFixedSize(self.width(), self.height()) # 禁止改变窗口边框大小 self.setWindowFlags(Qt.WindowCloseButtonHint)#禁止窗口帮助按钮 self.setWindowIcon(QIcon('img/logo.ico')) """布局""" self.frame=QFrame(self) self.frame.resize(300,175) self.frame.move(0,125) self.v_box_basic=QVBoxLayout(self.frame)#界面main self.lose_h_box = QHBoxLayout() # 登陆失败提示标签 self.v_box_basic.addLayout(self.lose_h_box) self.lw_h_box=QHBoxLayout()#标签及文本框 self.v_box_basic.addLayout(self.lw_h_box) self.nw_v_box=QVBoxLayout()#账户和密码标签 self.lw_h_box.addLayout(self.nw_v_box) self.t_v_box=QVBoxLayout()#账户和密码文本框 self.lw_h_box.addLayout(self.t_v_box) self.login_h_box = QHBoxLayout()#登陆按钮 self.v_box_basic.addLayout(self.login_h_box) """头像""" self.label_user=QLabel(self) self.label_user.resize(125,125) self.label_user.move(90,0) png=QPixmap('img/登陆头像3.png') self.label_user.setPixmap(png) """提示标签""" self.label_lose = QLabel() self.lose_h_box.addWidget(self.label_lose) """账户和密码标签""" self.label_user_name=QLabel("账户:") self.nw_v_box.addWidget(self.label_user_name) self.label_user_word = QLabel("密码:") self.nw_v_box.addWidget(self.label_user_word) """用户名和密码文本框""" self.text_user_name=QLineEdit() self.text_user_name.setPlaceholderText("请输入账号") self.text_user_name.setStyleSheet("QLineEdit{border:0px solid;border-radius:10%;box-shadow:10px 10px 5px #888888;}" "QLineEdit:hover{border:2px solid;}") self.t_v_box.addWidget(self.text_user_name) self.text_user_word=QLineEdit() self.text_user_word.setPlaceholderText("请输入密码") self.text_user_word.setStyleSheet("QLineEdit{border:0px solid;border-radius:10%;box-shadow:10px 10px 5px #888888;}" "QLineEdit:hover{border:2px solid;}") self.t_v_box.addWidget(self.text_user_word) """登陆按钮""" self.login_button=QPushButton() self.login_button.setStyleSheet("QPushButton{background-image: url(img/button_login.png); border-width: 10px;border-height: 100px;border-style: solid;}" "QPushButton:hover{background-image: url(img/button_login_hover.png)}") self.login_button.setMinimumSize(120,60) self.login_button.setMaximumSize(120,60) self.login_h_box.addWidget(self.login_button) self.v_box_basic.setAlignment(Qt.AlignCenter) self.login_button.clicked.connect(lambda :lr._login_main(login)) self.setWindowTitle("login") self.show() """登陆程序""" class LoginRegister(): def __init__(self): self.names="root"#获取数据库用户名 self.wordpasss="yidaimingjvn2017"#获取数据库用户密码 """登陆判断""" def _login_main(self,login,winmain=None): name=login.text_user_name.text()#获取账户文本框内容 wordpass=login.text_user_word.text()#获取密码文本框内容 if len(name)>0: if name==name: if wordpass==self.wordpasss:#获取该账户密码并判断用户输入密码是否正确 print("登陆成功!") # winmain.window_main() a.win_main() login.close() else: login.label_lose.setText("您的账号或密码有误!") # print("您的账号或密码有误!") else: login.label_lose.setText("您的账号或密码有误!") # print("您的账号或密码有误!") if __name__ == '__main__': app=QApplication(sys.argv) lr = LoginRegister() dpt = DPT() balance = dpt.balance() id = dpt.id() a=App() login = Window_login() sys.exit(app.exec_()) ``` 数据库程序: ```python # coding:utf-8 import pymysql class DPT: def __init__(self): self.conn = None def conne(self): self.conn = pymysql.connect(host = "127.0.0.1",port = 3306, user="root", passwd = "yidaimingjvn2017", db = "个人", autocommit = True) self.cursor = self.conn.cursor(cursor = pymysql.cursors.DictCursor) def exe(self): self.cursor.execute("select * from money") def id(self): self.conne() self.exe() id = [] for id_a in self.cursor.fetchall(): id.append(id_a["id"]) return id def datatime(self): self.conne() self.exe() datatime = [] for dt_a in self.cursor.fetchall(): datatime.append(dt_a["日期时间"]) return datatime def income(self): self.conne() self.exe() income=[] for inc_a in self.cursor.fetchall(): income.append(inc_a["收入"]) return income def spending(self): self.conne() self.exe() spending = [] for sd_a in self.cursor.fetchall(): spending.append(sd_a["支出"]) return spending def sd_cause(self): self.conne() self.exe() sd_cause = [] for sdc_a in self.cursor.fetchall(): sd_cause.append(sdc_a["支出原因"]) return sd_cause def now_balance(self): self.conne() self.exe() now_balance = [] for nb_a in self.cursor.fetchall(): now_balance.append(nb_a["今日余额"]) return now_balance def balance(self): self.conne() self.exe() balance = [] for b_a in self.cursor.fetchall(): balance.append(b_a["总余额"]) return balance ```
Comments | NOTHING