Python语言实现的银行管理系统。带有界面,用户数据在数据库文件里实时变化。具有转账、查询、取款、存款、销户、注册、登录、改密,申诉,锁卡,背景音乐,电子时钟,爬取网站表情包等功能。自动创建文档来记录交易时间和交易金额。自动生成output文件夹来存储爬取网站的表情包。
代码繁琐,无心优化,多多见谅。
文末附源代码及相关资源,永久有效。)
import datetime,os,requests,sqlite3,random,threading,tkinter.messagebox,pygame as py,tkinter as tk, time,re
from PIL import Image, ImageTk
win = tk.Tk()
win.title('银行管理系统')
im = Image.open("photo.jpg")
img = ImageTk.PhotoImage(im)
imglabel = tk.Label(win, image=img).pack()
win.maxsize(550, 600)
win.minsize(550, 600)
start_lable = tk.Label(win, text='''
welcome to the south dust bank
这里是南尘银行,欢迎你的到来!
''', font=('宋体', 20))
start_lable.place(x=-110, y=290)
entry_adminuser = tk.Variable()
entry_admin = tk.Entry(win, text=entry_adminuser)
entry_admin.place(x=200, y=450, height=30)
tk.Label(win,text='name',font=('宋体',20)).place(x=120,y=445)
tk.Label(win,text='key',font=('宋体',20)).place(x=120,y=485)
entry_passwd = tk.Variable()
entry_adminpasswd = tk.Entry(win, text=entry_passwd, show='*')
entry_adminpasswd.place(x=200, y=490, height=30)
f=[0]
def admin_login():
if entry_admin.get() == '南尘':
if entry_adminpasswd.get() == '666':
admin_log()
else:
tk.messagebox.askretrycancel(title='提示', message='密码错误')
else:
tk.messagebox.askretrycancel(title='提示', message='管理员不存在')
button_login = tk.Button(win, text='管理员登录', font=('宋体', 15), command=admin_login)
button_login.place(x=210, y=405)
def update_time():
time.configure(text=datetime.datetime.now().strftime('%Y %m %d %H %M %S'))
time.after(1000, update_time)
time = tk.Label(win, font=('宋体', 20))
time.place(x=50, y=550)
update_time()
py.mixer.init()
py.mixer.music.load(r'.\videos\song.mp3')
def pause():
py.mixer.music.pause()
def unpause():
py.mixer.music.unpause()
def play():
py.mixer.music.play()
tk.Button(win,text='暂停',command=pause).place(x=30,y=400)
tk.Button(win,text='继续',command=unpause).place(x=30,y=460)
tk.Button(win,text='播放',command=play).place(x=30,y=520)
def admin_log():
k = 0
show = tk.Toplevel()
show.title('银行管理系统')
show.maxsize(550, 600)
show.minsize(550, 600)
img = Image.open('s.jpg')
photo = ImageTk.PhotoImage(img)
tk.Label(show, image=photo).pack()
tk.Label(show, text='passwd', font=('宋体', 15), bg='blue').place(x=310, y=300)
tk.Label(show, text='CardId', font=('宋体', 15), bg='purple').place(x=150, y=300)
User_CardId = tk.Variable()
entry_user = tk.Entry(show, text=User_CardId, bg='red')
entry_user.place(x=120, y=255)
User_PassWd = tk.Variable()
entry_mima = tk.Entry(show, text=User_PassWd, show='*', bg='yellow')
entry_mima.place(x=280, y=255)
def user_login():
k = 0
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute("create table if not exists Atm(cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone)")
cursor = c.execute("SELECT cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone from Atm")
for row in cursor:
if row[0] == entry_user.get():
cardId, cardPasswd, cardMoney, cardLock, name, IdCard, phone = row[0], row[1], row[2], row[3], row[4], \
row[5], row[6]
k = 1
if cardLock=='3':
tk.messagebox.askretrycancel(title='警告', message='密码错误输入三次已锁卡,可账户申诉解锁')
return 0
if entry_mima.get() == cardPasswd:
son_show = tk.Toplevel()
son_show.maxsize(550, 600)
son_show.minsize(550, 600)
img = Image.open('666.jpg')
photo = ImageTk.PhotoImage(img)
tk.Label(son_show, image=photo).pack()
son_show.title('银行管理系统')
money_save = tk.Variable()
entry_savemoney = tk.Entry(son_show, text=money_save,bg='brown')
entry_savemoney.place(x=210, y=183)
money_get = tk.Variable()
entry_getmoney = tk.Entry(son_show, text=money_get,bg='red')
entry_getmoney.place(x=210, y=233)
money_EFT = tk.Variable()
entry_EFTmoney = tk.Entry(son_show, text=money_EFT,bg='purple')
entry_EFTmoney.place(x=210, y=283)
money_EFTID = tk.Variable()
entry_EFTID = tk.Entry(son_show, text=money_EFTID,bg='yellow')
entry_EFTID.place(x=210, y=333)
change = tk.Variable()
entry_change = tk.Entry(son_show, text=change,bg='blue')
entry_change.place(x=210, y=383)
def deposit():
if entry_savemoney.get() == '':
tk.messagebox.showinfo('存款', '存款金额为0')
return 0
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute(
"create table if not exists Atm(cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone)")
cursor = c.execute("SELECT cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone from Atm")
for row in cursor:
if row[0] == entry_user.get():
cardId, cardPasswd, cardMoney, cardLock, name, IdCard, phone = row[0], row[1], row[2], \
row[3], row[4], \
row[5], row[6]
a = int((entry_savemoney.get()))
b = int(cardMoney)
e =str(a + b)
c.execute(
" update Atm set cardId='%s',cardPasswd='%s',cardMoney='%s',cardLock='%s',name='%s',IdCard='%s',phone='%s' where cardId='%s'" % (
cardId, cardPasswd, e, cardLock, name, IdCard, phone, entry_user.get()))
tk.messagebox.showinfo('存款', '存款金额为%s' % (entry_savemoney.get()))
node = datetime.datetime.now().strftime('%Y:%m:%d:%H:%M:%S')
node = str(node)
file = open('账本.txt', 'a+', encoding='utf-8')
file.write('\n %s %s存款金额为%s' % (node,entry_user.get(),entry_savemoney.get()))
file.close()
conn.commit()
conn.close()
def withdrawal():
if entry_getmoney.get() == '':
tk.messagebox.showinfo('取款','取款金额为0')
return 0
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute(
"create table if not exists Atm(cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone)")
cursor = c.execute("SELECT cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone from Atm")
for row in cursor:
if row[0] == entry_user.get():
cardId, cardPasswd, cardMoney, cardLock, name, IdCard, phone = row[0], row[1], row[2], \
row[3], row[4], \
row[5], row[6]
a = int(entry_getmoney.get())
b = int(cardMoney)
if a > b:
tk.messagebox.askretrycancel(title='警告', message='余额不足')
return 0
e = b - a
d = str(e)
c.execute(
" update Atm set cardId='%s',cardPasswd='%s',cardMoney='%s',cardLock='%s',name='%s',IdCard='%s',phone='%s' where cardId='%s'" % (
cardId, cardPasswd, d, cardLock, name, IdCard, phone, entry_user.get()))
tk.messagebox.showinfo('取款','取款金额为%s' % (entry_getmoney.get()))
node = datetime.datetime.now().strftime('%Y:%m:%d:%H:%M:%S')
node = str(node)
file = open('账本.txt', 'a+', encoding='utf-8')
file.write('\n %s %s取款金额为%s' % (node,entry_user.get(), entry_getmoney.get()))
file.close()
conn.commit()
conn.close()
def EFT():
if entry_EFTID.get() == '':
tk.messagebox.showwarning('警告','转账账户不能为空')
return 0
if entry_EFTmoney.get() == '':
tk.messagebox.showwarning('警告','金额为0转账失败')
return 0
k = 0
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute(
"create table if not exists Atm(cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone)")
cursor = c.execute("SELECT cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone from Atm")
for row in cursor:
if row[0] == entry_EFTID.get():
k = 1
EFT_cardId, EFT_cardPasswd, EFT_cardMoney, EFT_cardLock, EFT_name, EFT_IdCard, EFT_phone = \
row[0], row[1], row[2], row[3], row[4], row[5], row[6]
conn.close()
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute(
"create table if not exists Atm(cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone)")
cursor = c.execute("SELECT cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone from Atm")
for row in cursor:
if row[0] == entry_user.get():
cardId, cardPasswd, cardMoney, cardLock, name, IdCard, phone = \
row[0], row[1], row[2], row[3], row[4], row[5], row[6]
if k == 1:
a = int(entry_EFTmoney.get())
b = int(cardMoney)
if a > b:
tk.messagebox.askretrycancel(title='提示', message='余额不足')
return 0
e = b - a
d = str(e)
b1 = int(EFT_cardMoney)
e1 = b1 + a
d1 = str(e1)
c.execute(
" update Atm set cardId='%s',cardPasswd='%s',cardMoney='%s',cardLock='%s',name='%s',IdCard='%s',phone='%s' where cardId='%s'" % (
cardId, cardPasswd, d, cardLock, name, IdCard, phone, entry_user.get()))
c.execute(
" update Atm set cardId='%s',cardPasswd='%s',cardMoney='%s',cardLock='%s',name='%s',IdCard='%s',phone='%s' where cardId='%s'" % (
EFT_cardId, EFT_cardPasswd, d1, EFT_cardLock, EFT_name, EFT_IdCard, EFT_phone,
entry_EFTID.get()))
conn.commit()
conn.close()
tk.messagebox.showinfo(title='提示', message='已给%s成功转账%s元' % (entry_EFTID.get(), a))
node = datetime.datetime.now().strftime('%Y:%m:%d:%H:%M:%S')
node = str(node)
file = open('账本.txt', 'a+', encoding='utf-8')
file.write('\n %s %s向%s转账金额为%s' % (node, entry_user.get(), entry_EFTID.get(),a))
file.close()
return 0
if k == 0:
tk.messagebox.showinfo('提示','账户不存在')
return 0
def changepasswd():
mima = re.compile("[a-z]+|[A-Z]+").findall(entry_change.get())
if not mima or len(entry_change.get())<3:
tk.messagebox.showwarning('警告','新密码长度不能小于三且不能是纯数字')
return 0
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute(
" update Atm set cardId='%s',cardPasswd='%s',cardMoney='%s',cardLock='%s',name='%s',IdCard='%s',phone='%s' where cardId='%s'" % (
cardId, entry_change.get(), cardMoney, cardLock, name, IdCard, phone, entry_user.get()))
tk.messagebox.showinfo('改密', '密码已更改为%s重启生效' % (entry_change.get()))
conn.commit()
conn.close()
def kill():
check = tk.messagebox.askyesno(title='提示', message='are you sure')
if check:
node = datetime.datetime.now().strftime('%Y:%m:%d:%H:%M:%S')
node = str(node)
file = open('账本.txt', 'a+', encoding='utf-8')
file.write('\n %s 注销账户%s' % (node,entry_user.get()))
file.close()
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute("delete from Atm where cardId='%s'" % (entry_user.get()))
conn.commit()
conn.close()
son_show.destroy()
return 0
return 0
def query():
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute(
"create table if not exists Atm(cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone)")
cursor = c.execute("SELECT cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone from Atm")
for row in cursor:
if row[0] == entry_user.get():
cardId, cardPasswd, cardMoney, cardLock, name, IdCard, phone = row[0], row[1], row[2], \
row[3], row[4], \
row[5], row[6]
tk.messagebox.showinfo('余额', '你的账户余额%s' % (cardMoney))
conn.commit()
conn.close()
tk.Button(son_show, text='存款', font=('宋体', 10), command=deposit,bg='green').place(x=135, y=180)
tk.Button(son_show, text='取款', font=('宋体', 10), command=withdrawal,bg='yellow').place(x=135, y=230)
tk.Button(son_show, text='转账', font=('宋体', 10), command=EFT,bg='red').place(x=135, y=305)
tk.Button(son_show, text='改密', font=('宋体', 10), command=changepasswd,bg='brown').place(x=135, y=380)
tk.Button(son_show, text='销户', font=('宋体', 10), command=kill,bg='blue').place(x=220, y=430)
tk.Button(son_show, text='查询', font=('宋体', 10), command=query,bg='orange').place(x=300, y=430)
son_show.mainloop()
else:
if f[0]!=2:
f[0] = f[0] + 1
tk.messagebox.askretrycancel(title='警告', message='密码错误%d次'%(f[0]))
return
if f[0]==2:
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute(
" update Atm set cardId='%s',cardPasswd='%s',cardMoney='%s',cardLock='3',name='%s',IdCard='%s',phone='%s' where cardId='%s'" % (
cardId, cardPasswd, cardMoney,name, IdCard, phone, entry_user.get()))
conn.commit()
conn.close()
tk.messagebox.askretrycancel(title='警告', message='密码错误三次卡已被锁可申诉解锁')
return 0
if k == 0:
tk.messagebox.askretrycancel(title='提示', message='账户不存在')
def user_New():
son_show = tk.Toplevel()
son_show.maxsize(550, 600)
son_show.minsize(550, 600)
son_show.title('银行管理系统')
img = Image.open('4.jpg')
photo = ImageTk.PhotoImage(img)
tk.Label(son_show, image=photo).pack()
tk.Label(son_show, text='UserName', font=('宋体', 15), bg='orange').place(x=10, y=15)
tk.Label(son_show, text='UserId', font=('宋体', 15), bg='brown').place(x=10, y=55)
tk.Label(son_show, text='phone', font=('宋体', 15), bg='cyan').place(x=10, y=95)
tk.Label(son_show, text='passwd', font=('宋体', 15), bg='pink').place(x=10, y=135)
user_name = tk.Variable()
entry_name = tk.Entry(son_show, text=user_name, bg='pink')
entry_name.place(x=100, y=15, height=25)
user_Id = tk.Variable()
entry_userId = tk.Entry(son_show, text=user_Id, bg='cyan')
entry_userId.place(x=100, y=55, height=25)
user_phone = tk.Variable()
entry_phone = tk.Entry(son_show, text=user_phone, bg='brown')
entry_phone.place(x=100, y=95, height=25)
cardPasswd = tk.Variable()
entry_cardPasswd = tk.Entry(son_show, text=cardPasswd, show='*', bg='orange')
entry_cardPasswd.place(x=100, y=135, height=25)
card_Id = randomCardId()
card_lock = '0'
card_Money = '0'
def save():
ret = re.match(r"^1[35678]\d{9}$", entry_phone.get())
match=re.compile('[0-9]+').findall(entry_name.get())
pipei=re.match(r"^372922[1-2]\d{11}$",entry_userId.get())
mima=re.compile("[a-z]+|[A-Z]+").findall(entry_cardPasswd.get())
if not ret or match or not pipei or len(entry_cardPasswd.get())<3 or not mima:
tk.messagebox.showwarning('提示', '输入无效,注册时名字不能包含数字,手机号和身份证号应符合国家规定,密码长度不能为纯数字且不能低于三')
return 0
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute("create table if not exists Atm(cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone)")
c.execute(" insert into Atm values ('%s','%s','%s','%s','%s','%s','%s' )" % (
card_Id, entry_cardPasswd.get(), card_Money, card_lock, entry_name.get(), entry_userId.get(),
entry_phone.get()))
conn.commit()
conn.close()
tk.messagebox.showinfo('提示','恭喜你注册成功,卡号为%s' % (card_Id))
node = datetime.datetime.now().strftime('%Y:%m:%d:%H:%M:%S')
node = str(node)
file = open('账本.txt', 'a+', encoding='utf-8')
file.write('\n %s %s 注册成功卡号为%s' % (node, entry_name.get(),card_Id))
file.close()
button = tk.Button(son_show, text='保存', command=save,bg='purple')
button.place(x=125, y=180)
tk.Button(son_show,text='退出',command=lambda :son_show.destroy(),bg='brown').place(x=180,y=180)
son_show.mainloop()
def randomCardId():
while True:
str = ""
for i in range(8):
ch = chr(random.randrange(ord('0'), ord('9') + 1))
str = str + ch
return str
def GUI_son():
son_GUI = tk.Toplevel()
son_GUI.maxsize(550, 600)
son_GUI.minsize(550, 600)
son_GUI.title('银行管理系统')
im = Image.open("1.jpg")
img = ImageTk.PhotoImage(im)
imglabel = tk.Label(son_GUI, image=img).pack()
tk.Label(son_GUI, text='银行系统用户如下:', font=('宋体', 20)).place(x=10, y=10)
i = 40
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute("create table if not exists Atm(cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone)")
cursor = c.execute("SELECT cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone from Atm")
for row in cursor:
tk.Label(son_GUI, text='%s' % (row[0]), font=('宋体', 20), bg='orange').place(x=250, y=i)
i = i + 40
son_GUI.mainloop()
def user_find():
tk.messagebox.showinfo('提示','请输入名字电话身份证号进行申诉')
user_name = tk.Variable()
entry_name = tk.Entry(show, text=user_name, bg='pink')
entry_name.place(x=120, y=100, height=25)
tk.Label(show,text='UserName',bg='blue').place(x=30,y=100)
user_Id = tk.Variable()
entry_userId = tk.Entry(show, text=user_Id, bg='cyan')
entry_userId.place(x=120, y=150, height=25)
tk.Label(show, text='UserId',bg='red').place(x=50, y=150)
user_phone = tk.Variable()
entry_phone = tk.Entry(show, text=user_phone, bg='brown')
entry_phone.place(x=120, y=200, height=25)
tk.Label(show, text='phone',bg='yellow').place(x=50, y=200)
def User_check():
conn = sqlite3.connect('user2.db')
c = conn.cursor()
c.execute(
"create table if not exists Atm(cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone)")
cursor = c.execute("SELECT cardId,cardPasswd,cardMoney,cardlock,name ,IdCard,phone from Atm")
for row in cursor:
if row[4]== entry_name.get() and row[5] == entry_userId.get() and row[6] == entry_phone.get() :
str = ""
for i in range(2):
ch = chr(random.randrange(48, 58))
ch1 = chr(random.randrange(65, 91))
ch2 = chr(random.randrange(97, 123))
str = str + ch + ch1 + ch2
unlock='0'
c.execute(
" update Atm set cardPasswd='%s',cardlock='%s' where cardId ='%s'" %(str,unlock,entry_user.get()))
conn.commit()
conn.close()
tk.messagebox.showwarning('提示','申诉成功,系统已随机重置密码为%s'%(str))
return 0
tk.messagebox.showwarning('警告','很遗憾申诉失败')
return 0
tk.Button(show,text='验证',command=lambda :User_check(),bg='brown').place(x=250,y=300)
menubar = tk.Menu(show)
filemenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='操作命令', menu=filemenu)
filemenu.add_command(label='注册', command=user_New)
filemenu.add_command(label='用户', command=GUI_son)
filemenu.add_command(label='登录', command=user_login)
filemenu.add_command(label='申诉',command=user_find)
filemenu.add_command(label='退出', command=lambda: show.destroy())
show.config(menu=menubar)
show.mainloop()
def pachong():
file_path = 'output'
if not os.path.exists(file_path):
os.mkdir(file_path)
from bs4 import BeautifulSoup
def download_all_htmls():
htmls = []
for idx in range(1):
url = f"https://www.fabiaoqing.com/biaoqing/lists/page/{idx + 1}.html"
print("craw html", url)
r = requests.get(url)
if r.status_code != 200:
raise Exception('error')
htmls.append(r.text)
print('success')
return htmls
htmls = download_all_htmls()
def parse_single_html(html):
soup = BeautifulSoup(html, 'html.parser')
img_divs = soup.find_all('div', class_='tagbqppdiv')
datas = []
for img_div in img_divs:
img_node = img_div.find('img')
if not img_node: continue
datas.append((img_node['title'], img_node['data-original']))
return datas
import pprint
pprint.pprint(parse_single_html(htmls[0])[:10])
all_imgs = []
for html in htmls:
all_imgs.extend(parse_single_html(html))
for idx, (title, img_url) in enumerate(all_imgs):
reg = '[^0-9A-Za-z\u4e00-\u9fa5]'
title = re.sub(reg, '', title)
if len(title) > 20: title = title[:10]
post_fix = img_url[-3:]
filename = f'./output/{title}.{post_fix}'
print(idx, filename)
img_data = requests.get(img_url)
with open(filename, 'wb') as f:
f.write(img_data.content)
def pas():
a=tk.messagebox.askyesno(title='提示',message='确定要下载表情包?')
if a:
t = threading.Thread(target=pachong)
t.setDaemon(True)
t.start()
return 0
return 0
tk.Button(win,text='福利',command=pas).place(x=400,y=410)
win.mainloop()
链接:https://pan.baidu.com/s/1Z1uLQANHdpTDOeJzZ14wRg
提取码:vr8g