python的ATM取款机项目用PySimpleGUI前段和后端 有注册登录存款取款转账的功能
时间: 2024-03-26 14:35:40 浏览: 123
好的,下面是一个使用PySimpleGUI实现的简单ATM取款机项目,包括注册、登录、存款、取款和转账功能:
```python
import PySimpleGUI as sg
# 初始用户列表
users = [{'username': 'admin', 'password': 'admin123', 'balance': 10000},
{'username': 'user1', 'password': 'pass1', 'balance': 5000},
{'username': 'user2', 'password': 'pass2', 'balance': 3000}]
# 登录窗口
def login_window():
layout = [[sg.Text('用户名:'), sg.Input(key='username')],
[sg.Text('密码:'), sg.Input(key='password', password_char='*')],
[sg.Button('登录'), sg.Button('注册')]]
window = sg.Window('登录', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == '登录':
username = values['username']
password = values['password']
for user in users:
if user['username'] == username and user['password'] == password:
sg.popup('登录成功!')
window.close()
main_window(user)
return
sg.popup('登录失败!用户名或密码错误。')
elif event == '注册':
window.close()
register_window()
return
window.close()
# 注册窗口
def register_window():
layout = [[sg.Text('用户名:'), sg.Input(key='username')],
[sg.Text('密码:'), sg.Input(key='password', password_char='*')],
[sg.Text('确认密码:'), sg.Input(key='confirm_password', password_char='*')],
[sg.Button('注册')]]
window = sg.Window('注册', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == '注册':
username = values['username']
password = values['password']
confirm_password = values['confirm_password']
if password != confirm_password:
sg.popup('注册失败!两次输入的密码不一致。')
else:
users.append({'username': username, 'password': password, 'balance': 0})
sg.popup('注册成功!请登录。')
window.close()
login_window()
return
window.close()
# 主窗口
def main_window(user):
layout = [[sg.Text('用户名:{}'.format(user['username']))],
[sg.Text('余额:{}元'.format(user['balance']))],
[sg.Button('存款'), sg.Button('取款'), sg.Button('转账'), sg.Button('退出')]]
window = sg.Window('ATM取款机', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == '退出':
break
if event == '存款':
amount = sg.popup_get_text('请输入存款金额:')
if amount is None:
continue
try:
amount = int(amount)
if amount <= 0:
raise ValueError()
user['balance'] += amount
sg.popup('存款成功!当前余额为:{}元。'.format(user['balance']))
except ValueError:
sg.popup('存款失败!请输入一个正整数。')
elif event == '取款':
amount = sg.popup_get_text('请输入取款金额:')
if amount is None:
continue
try:
amount = int(amount)
if amount <= 0:
raise ValueError()
if amount > user['balance']:
raise Exception('余额不足。')
user['balance'] -= amount
sg.popup('取款成功!当前余额为:{}元。'.format(user['balance']))
except ValueError:
sg.popup('取款失败!请输入一个正整数。')
except Exception as e:
sg.popup('取款失败!{}'.format(e))
elif event == '转账':
username = sg.popup_get_text('请输入对方用户名:')
if username is None:
continue
for u in users:
if u['username'] == username:
amount = sg.popup_get_text('请输入转账金额:')
if amount is None:
continue
try:
amount = int(amount)
if amount <= 0:
raise ValueError()
if amount > user['balance']:
raise Exception('余额不足。')
user['balance'] -= amount
u['balance'] += amount
sg.popup('转账成功!当前余额为:{}元。'.format(user['balance']))
except ValueError:
sg.popup('转账失败!请输入一个正整数。')
except Exception as e:
sg.popup('转账失败!{}'.format(e))
break
else:
sg.popup('转账失败!对方用户名不存在。')
window.close()
# 运行程序
login_window()
```
这个项目使用了PySimpleGUI实现了界面交互,并且使用了一个简单的用户列表来存储用户信息。你可以根据自己的需求对它进行修改和扩展。
阅读全文