|
楼主 |
发表于 2020-2-10 11:39:44
|
显示全部楼层
本帖最后由 dlnb526 于 2020-2-11 11:10 编辑
main.py# -*- coding: utf-8 -*-
"""
Created on Sun Feb 9 14:09:39 2020
@author: dlnb5
"""
#账单主程序
from books_register import Login
from tkinter import *
import time
import json
from history_list import Historylist
from tkinter import filedialog
class MoneyBook():
def __init__(self):
login = Login()
judge1 = 0
try:
self.Total = 0
self.data = login.data1
self.username = login.user_name
print('您好:%s,欢迎使用账本!控制台将显示您的操作历史'%self.username)
print('当前余额:%.2f'%self.data[-1]["Total"])
self.Total = self.data[-1]["Total"]
judge1 = 1
except:
print('处理错误,请重新注册!')
if judge1 == 1:
self.bookGui()
#检测输入框里是否输入数字
def inputwindow(self,select):
print('%s'%select)
self.labelframe2.grid_forget()
self.labelframe2 = LabelFrame(self.root,text='%s'%select,labelanchor=N,padx=80,pady=50)
self.labelframe2.grid(row=1,column=1,padx=5,pady=10)
self.label_item = Label(self.labelframe2,text = '%s项目:'%select,padx=5,pady=5)
self.label_item.grid(row=2,column=1)
self.label_price = Label(self.labelframe2,text = '金额:',padx=5,pady=5)
self.label_price.grid(row=3,column=1)
self.entry_item = Entry(self.labelframe2)
self.entry_item.grid(row=2,column=2,padx=10,pady=5)
self.entry_price = Entry(self.labelframe2)
self.entry_price.grid(row=3,column=2,padx=10,pady=5)
self.savebutton =Button(self.labelframe2,text='保存',command = self.save)
self.savebutton.grid(row=4, column=3)
def income(self):
self.inputwindow('收入')
self.choice = 1
def outcome(self):
self.inputwindow('支出')
self.choice = 2
#保存数据
def save(self):
if self.choice == 1:
print('正在执行保存收入操作')
try:
self.price_input = self.entry_price.get()
self.item = self.entry_item.get()
self.price_input=float(self.price_input)
self.atime=time.localtime()
self.timstr='%d%02d%02d%02d%02d%02d'%(self.atime[0],self.atime[1],self.atime[2],self.atime[3],self.atime[4],self.atime[5])
print('-------当前时间为:%s-------'%self.timstr)
self.Total = self.Total + self.price_input
self.dict_temp = {"Time":self.timstr,"Item":self.item,"Exp":self.price_input,"Total":self.Total}
self.data.append(self.dict_temp)
with open('user_data\%s_file.json'%self.username,'w', encoding='utf-8') as file2:
json.dump(self.data, file2, indent=4)
self.label3.grid_forget()
self.label3 = Label(self.root,text='%02d:%02d:%02d 保存成功!收入%.2f元,余额%.2f元'%(self.atime[3],self.atime[4],self.atime[5],self.price_input,self.Total),padx=5)
self.label3.grid(row =4,column=1,padx=5,pady=1)
except:
print('输入有误!')
self.label3.grid_forget()
self.label3 = Label(self.root,text='金额处请输入数字!',padx=5,fg='red')
self.label3.grid(row =4,column=1,padx=5,pady=1)
elif self.choice == 2:
print('正在执行保存支出')
try:
self.price_input = self.entry_price.get()
self.item = self.entry_item.get()
self.price_input=-float(self.price_input)
self.atime=time.localtime()
self.timstr='%d%02d%02d%02d%02d%02d'%(self.atime[0],self.atime[1],self.atime[2],self.atime[3],self.atime[4],self.atime[5])
print('-------当前时间为:%s-------'%self.timstr)
self.Total = self.Total + self.price_input
self.dict_temp = {"Time":self.timstr,"Item":self.item,"Exp":self.price_input,"Total":self.Total}
self.data.append(self.dict_temp)
with open('user_data\%s_file.json'%self.username,'w', encoding='utf-8') as file2:
json.dump(self.data, file2, indent=4)
self.label3.grid_forget()
self.label3 = Label(self.root,text='%02d:%02d:%02d 保存成功!支出%.2f元,余额%.2f元'%(self.atime[3],self.atime[4],self.atime[5],-self.price_input,self.Total),padx=5)
self.label3.grid(row =4,column=1,padx=5,pady=1)
except:
print('输入有误!')
self.label3.grid_forget()
self.label3 = Label(self.root,text='金额处请输入数字!',padx=5,fg='red')
self.label3.grid(row =4,column=1,padx=5,pady=1)
def history(self):
Historylist(self.username)
#设计账本的图形界面
def bookGui(self):
self.root = Tk()
self.root.title('%s的账本 by dlnb526'%self.username)
self.labelframe1 = LabelFrame(self.root,text="记录",labelanchor=N,padx=30,pady=20)
self.labelframe1.grid(row=1,column=0,padx=5,pady=10)
self.button1 = Button(self.labelframe1,text='收入',command = self.income)
self.button1.grid(row=2,column=0,padx=5,pady=10)
self.button2 = Button(self.labelframe1,text='支出',command=self.outcome)
self.button2.grid(row=3,column=0,padx=5,pady=10)
self.labelframe2 = LabelFrame(self.root,text="请选择",labelanchor=N,padx=80,pady=50)
self.labelframe2.grid(row=1,column=1,padx=5,pady=10)
self.label2 = Label(self.labelframe2,text='请在左侧选择内容',padx=20,pady=20)
self.label2.grid(row=1,column=4,padx=5,pady=5)
self.result=StringVar()
self.result.set('当前总计余额%.2f'%self.Total)
self.label3 = Label(self.root,textvariable=self.result,padx=5)
self.label3.grid(row =4,column=1,padx=5,pady=1)
self.button3 = Button(self.root,text='查看历史账单',command=self.history)
self.button3.grid(row =5,column=1,padx=5,pady=5)
mainloop()
moneybook = MoneyBook()
history_list.py# -*- coding: utf-8 -*-
"""
Created on Sun Feb 2020
@author: dlnb5
"""
#账单历史的程序
from tkinter import *
import json
from tkinter import filedialog
class Historylist():
def __init__(self,username):
print(username)
self.username = username
print(' 您打开了账单历史页面,正在写入文件')
with open('user_data\%s_file.json'%self.username,'r') as file1:
self.data1 = json.loads(file1.read())
self.hlwindow()
def search(self):
print('正在载入搜索功能')
print(self.entry1.get())
self.text.delete(1.0,END)
self.searchcontent = self.entry1.get()
self.text.insert(END,'搜索结果如下!\n')
self.count=0
for eachdict in self.data1:
temp_data = eachdict["Time"]
if self.searchcontent in temp_data:
self.count=1
try:
self.text.insert(END,'您在%s 收支了%.2f元,\n项目是%s\n'%(eachdict["Time"],eachdict["Exp"],eachdict["Item"]))
except:
self.text.insert(END,'\n%s缺少项目无法展示\n'%eachdict["Time"])
if self.count == 0:
self.text.insert(END,'没有搜索结果')
self.text.insert(END,'\n-------\n搜索结束')
def saveas(self):
print('正在载入另存为~')
filename = filedialog.asksaveasfilename(title='选择要保存账单的位置', filetypes=[('All Files', '*.txt')])
print(filename)
with open(filename,'w') as file2:
file2.write('\n欢迎使用账单程序 by dlnb526\n')
for eachdict in self.data1:
try:
file2.write('-----------------------------\n时间 %s\n项目 %s \n收支 %.2f元\n余额 %.2f元\n'%\
(eachdict['Time'],eachdict['Item'],eachdict['Exp'],eachdict['Total']))
except:
file2.write('\n%s缺少项目无法展示\n'%eachdict["Time"])
def datapro(self):
self.text.delete(1.0,END)
self.text.insert(END,'****%s,您的历史账单如下:****\n'%self.username)
for eachdict in self.data1:
try:
self.hisstr = '-----------------------------\n时间 %s\n项目 %s \n收支 %.2f元\n余额 %.2f元\n'%\
(eachdict['Time'],eachdict['Item'],eachdict['Exp'],eachdict['Total'])
self.text.insert(INSERT,self.hisstr)
except:
print('emmm已经录入数据')
self.text.insert(END,'---------\n感谢使用本程序!')
def back(self):
self.datapro()
#账单历史的界面
def hlwindow(self):
self.root=Tk()
self.root.title("历史账单 by dlnb526")
self.labelframe = LabelFrame(self.root,text="历史账单如下",labelanchor=N)
self.labelframe.grid(padx=10,pady=10,columnspan=4)
self.sb_text = Scrollbar(self.labelframe)
self.sb_text.grid(row=2,column=4,sticky=N+S)
self.text = Text(self.labelframe,width=50, height=30,yscrollcommand =self.sb_text.set)
self.text.grid(row=2,column=1,columnspan=3,padx=10,pady=10,sticky=W)
self.datapro()
self.label1 = Label(self.root,text="请输入要搜索的日期")
self.label1.grid(row=3,column=0,sticky=W,padx=5)
self.entry1 = Entry(self.root)
self.entry1.grid(row=3,column=1)
self.button2 = Button(self.root,text = "另存为",command=self.saveas)
self.button2.grid(padx=30,pady=30,row=4,column=1,columnspan=3,sticky=W+E)
self.button1 = Button(self.root,text = "搜索",command=self.search)
self.button1.grid(row=3,column=2,padx=10,pady=10,sticky=W)
self.button3 = Button(self.root,text = "刷新账单",command=self.back)
self.button3.grid(row=3,column=3,padx=10,pady=10,sticky=E)
self.sb_text.config(command=self.text.yview)
mainloop()
books_register.py# -*- coding: utf-8 -*-
"""
Created on Sat Feb 8 21:14:34 2020
@author: dlnb5
"""
#利用tkinter实现登陆程序界面for账单
from tkinter import *
import tkinter.messagebox
import webbrowser
import pickle
import os
import os.path
import json
class Login:
def __init__(self):
print('您已载入登陆程序')
#首先进行相关文件处理
if os.path.exists('User_dict.pkl'):
file1 = open('User_dict.pkl','rb')
self.temp_dict = pickle.load(file1)
file1.close()
elif not os.path.exists('User_dict.pkl'):
self.temp_dict={'username':'password'}
file1 = open('User_dict.pkl','wb')
pickle.dump(self.temp_dict,file1)
file1.close()
self.judgeframe3 = 0
#然后生成图形界面
self.root = Tk()
self.root.title('登陆 by dlnb526')
self.labelframe1 = LabelFrame(self.root,text='欢迎!',padx=5,pady=5,\
labelanchor = N)
self.labelframe1.grid(row=0,column=0,padx=10,pady=10)
self.label1 = Label(self.labelframe1,text = "欢迎来到本程序!\n注意本程序密码明文保存,注意隐私安全",padx=5,pady=5)
self.label1.grid(row = 0,column=0,padx=5,pady=5)
self.anoutME = Button(self.labelframe1,text = '关于我',command = self.about_me)
self.anoutME.grid(row=1,column=1)
self.labelframe2 = LabelFrame(self.root,text="登录程序",padx=30,pady=30)
self.labelframe2.grid(row=0,column=1,padx=10,pady=10)
self.label2 = Label(self.labelframe2,text = "这是一个登录程序的演示!",padx=5,pady=5)
self.label2.grid(row = 0,column=1,padx=5,pady=5)
self.button1 = Button(self.labelframe2,text = '新建账户',command = self.newAccount)
self.button1.grid(row=1,column=1)
self.button2 = Button(self.labelframe2,text = '已有帐户登录',command = self.hadAccount)
self.button2.grid(row=1,column=2)
mainloop()
#---------------------------------新建账户后的行为----------------------------------------
def newAccount(self):
self.creatAccount()
def creatAccount(self):
if self.judgeframe3 == 1:
self.labelframe3.grid_forget()
self.judgeframe3 = 0
self.labelframe3 = LabelFrame(self.root,text="注册登录",padx=30,pady=30)
self.labelframe3.grid(row=2,column=1,padx=10,pady=10)
self.judgeframe3 = 1
self.label_username = Label(self.labelframe3,text = '用户名:',padx=5,pady=5)
self.label_username.grid(row=2,column=1)
self.label_password1 = Label(self.labelframe3,text = '请输入密码:',padx=5,pady=5)
self.label_password1.grid(row=3,column=1)
self.label_password2 = Label(self.labelframe3,text = '请再次输入密码:',padx=5,pady=5)
self.label_password2.grid(row=4,column=1)
self.entry_username = Entry(self.labelframe3)
self.entry_username.grid(row=2,column=2,padx=10,pady=5)
self.entry_password1 = Entry(self.labelframe3,show='*')
self.entry_password1.grid(row=3,column=2,padx=10,pady=5)
self.entry_password2 = Entry(self.labelframe3,show='*')
self.entry_password2.grid(row=4,column=2,padx=10,pady=5)
self.regist_button = Button(self.labelframe3,text = '注册',command = self.regist_new_Account)
self.regist_button.grid(row=5,column=2,padx=10,pady=5)
#点击注册后的行为
def regist_new_Account(self):
self.user_name = self.entry_username.get()
self.password1 = self.entry_password1.get()
self.password2 = self.entry_password2.get()
if self.user_name in self.temp_dict:
self.label3 = Label(self.labelframe3,text = "账号已存在",padx=5,pady=5,fg='red')
self.label3.grid(row = 5,column=1,padx=5,pady=5)
else:
if self.password1==self.password2:
self.password = self.password1
if self.password:
self.dict1 = {self.user_name:self.password}
self.temp_dict.update(self.dict1)
file1 = open('User_dict.pkl','wb')
pickle.dump(self.temp_dict,file1)
file1.close()
self.file_creat()
self.label3 = Label(self.labelframe3,text = "注册成功!\n请返回登陆",padx=5,pady=5,fg='red')
self.label3.grid(row = 5,column=1,padx=5,pady=5)
else:
self.label3 = Label(self.labelframe3,text = "密码不能为空!",padx=5,pady=5,fg='red')
self.label3.grid(row = 5,column=1,padx=5,pady=5)
else:
self.label3 = Label(self.labelframe3,text = "前后两次密码输入不一致",padx=5,pady=5,fg='red')
self.label3.grid(row = 5,column=1,padx=5,pady=5)
def file_creat(self):
if os.path.exists('user_data'):
dict_list = [{"Total":0,"Time": '00000000000000',"Item":'BLANK',"Exp":0}]
with open('user_data\%s_file.json'%self.user_name,'w', encoding='utf-8') as file2:
json.dump(dict_list, file2, indent=4)
if not os.path.exists('user_data'):
os.mkdir('user_data')
dict_list = [{"Total":0,"Time": '00000000000000',"Item":'BLANK',"Exp":0}]
with open('user_data\%s_file.json'%self.user_name,'w', encoding='utf-8') as file2:
json.dump(dict_list, file2, indent=4)
#------------------------------------点击登陆后的行为--------------------------------------------------
def hadAccount(self):
if self.judgeframe3 == 1:
self.labelframe3.grid_forget()
self.judgeframe3 = 0
self.labelframe3 = LabelFrame(self.root,text="登录",padx=30,pady=50)
self.labelframe3.grid(row=2,column=1,padx=10,pady=10)
self.label_username0 = Label(self.labelframe3,text = '用户名:',padx=5,pady=5)
self.label_username0.grid(row=2,column=1)
self.label_password0 = Label(self.labelframe3,text = '请输入密码:',padx=5,pady=5)
self.label_password0.grid(row=3,column=1)
self.entry_username_0 = Entry(self.labelframe3)
self.entry_username_0.grid(row=2,column=2,padx=10,pady=5)
self.entry_password_0 = Entry(self.labelframe3,show='*')
self.entry_password_0.grid(row=3,column=2,padx=10,pady=5)
self.judgeframe3 = 1
self.regist_button = Button(self.labelframe3,text = '登录',command = self.check_login)
self.regist_button.grid(row=5,column=2,padx=10,pady=5)
def check_login(self):
self.user_name = self.entry_username_0.get()
self.input_password = self.entry_password_0.get()
if self.user_name in self.temp_dict:
if self.temp_dict[self.user_name] == self.input_password:
self.label3 = Label(self.labelframe3,text = "登陆成功~\n正在加载程序",padx=5,pady=5,fg='red')
self.label3.grid(row = 5,column=1,padx=5,pady=5)
self.file_import()
print('登陆成功!正在载入数据……')
else:
self.label3 = Label(self.labelframe3,text = "密码错误!",padx=5,pady=5,fg='red')
self.label3.grid(row = 5,column=1,padx=5,pady=5)
else:
self.label3 = Label(self.labelframe3,text = "用户名不存在!",padx=5,pady=5,fg='red')
self.label3.grid(row = 5,column=1,padx=5,pady=5)
def file_import(self):
try:
with open('user_data\%s_file.json'%self.user_name,'r') as file2:
self.data1 = json.loads(file2.read())
print('数据加载成功')
#关闭窗口
self.root.destroy()
print('登陆成功,已经打开账本程序')
except:
tkinter.messagebox.showerror('错误','数据读取错误')
def about_me(self):
webbrowser.open('https://fishc.com.cn/space-uid-783916.html')
|
|