用python写一个ATM取款机项目PySimpleGUI前段和后端 有注册登录存款取款转账的功能
时间: 2024-03-26 13:37:08 浏览: 33
好的,下面是一个使用Python和PySimpleGUI实现的ATM取款机项目,包含了注册、登录、存款、取款和转账等功能。代码如下:
```python
import PySimpleGUI as sg
# 定义窗口布局
layout_login = [
[sg.Text('欢迎使用ATM取款机')],
[sg.Text('请输入账号'), sg.Input(key='-ACCOUNT-')],
[sg.Text('请输入密码'), sg.Input(key='-PASSWORD-', password_char='*')],
[sg.Button('登录'), sg.Button('注册')],
]
layout_main = [
[sg.Text('请选择操作')],
[sg.Button('查询余额'), sg.Button('取款')],
[sg.Button('存款'), sg.Button('转账')],
[sg.Button('退出')]
]
layout_register = [
[sg.Text('欢迎注册')],
[sg.Text('请输入账号'), sg.Input(key='-ACCOUNT-')],
[sg.Text('请输入密码'), sg.Input(key='-PASSWORD-', password_char='*')],
[sg.Button('确认注册')],
]
layout_deposit = [
[sg.Text('请输入存款金额')],
[sg.Input(key='-DEPOSIT-')],
[sg.Button('确认存款')],
]
layout_withdraw = [
[sg.Text('请输入取款金额')],
[sg.Input(key='-WITHDRAW-')],
[sg.Button('确认取款')],
]
layout_transfer = [
[sg.Text('请输入转账账号'), sg.Input(key='-TO-')],
[sg.Text('请输入转账金额'), sg.Input(key='-AMOUNT-')],
[sg.Button('确认转账')],
]
# 创建窗口
window_login = sg.Window('ATM取款机', layout_login)
window_main = None
window_register = None
window_deposit = None
window_withdraw = None
window_transfer = None
# 用户信息
user_info = {
'1001': {
'password': '123456',
'balance': 1000,
},
'1002': {
'password': '123456',
'balance': 2000,
}
}
# 当前登录用户
current_user = None
# 进入登录界面
while True:
event, values = window_login.read()
if event == sg.WIN_CLOSED:
break
if event == '登录':
account = values['-ACCOUNT-']
password = values['-PASSWORD-']
if account in user_info and password == user_info[account]['password']:
current_user = account
window_login.hide()
window_main = sg.Window('ATM取款机', layout_main)
break
else:
sg.popup('账号或密码错误')
if event == '注册':
window_login.hide()
window_register = sg.Window('ATM取款机', layout_register)
# 进入主界面
while window_main is not None:
event, values = window_main.read()
if event == sg.WIN_CLOSED or event == '退出':
window_main.close()
break
if event == '查询余额':
sg.popup(f'账户余额为{user_info[current_user]["balance"]}')
if event == '取款':
window_main.hide()
window_withdraw = sg.Window('ATM取款机', layout_withdraw)
if event == '存款':
window_main.hide()
window_deposit = sg.Window('ATM取款机', layout_deposit)
if event == '转账':
window_main.hide()
window_transfer = sg.Window('ATM取款机', layout_transfer)
# 进入注册界面
while window_register is not None:
event, values = window_register.read()
if event == sg.WIN_CLOSED:
window_register.close()
window_login.un_hide()
break
if event == '确认注册':
account = values['-ACCOUNT-']
password = values['-PASSWORD-']
if account in user_info:
sg.popup('该账号已存在')
else:
user_info[account] = {
'password': password,
'balance': 0,
}
sg.popup('注册成功')
window_register.close()
window_login.un_hide()
# 进入存款界面
while window_deposit is not None:
event, values = window_deposit.read()
if event == sg.WIN_CLOSED:
window_deposit.close()
window_main.un_hide()
break
if event == '确认存款':
amount = int(values['-DEPOSIT-'])
user_info[current_user]['balance'] += amount
sg.popup(f'存款成功,当前余额为{user_info[current_user]["balance"]}')
window_deposit.close()
window_main.un_hide()
# 进入取款界面
while window_withdraw is not None:
event, values = window_withdraw.read()
if event == sg.WIN_CLOSED:
window_withdraw.close()
window_main.un_hide()
break
if event == '确认取款':
amount = int(values['-WITHDRAW-'])
if amount <= user_info[current_user]['balance']:
user_info[current_user]['balance'] -= amount
sg.popup(f'取款成功,当前余额为{user_info[current_user]["balance"]}')
window_withdraw.close()
window_main.un_hide()
else:
sg.popup('余额不足')
# 进入转账界面
while window_transfer is not None:
event, values = window_transfer.read()
if event == sg.WIN_CLOSED:
window_transfer.close()
window_main.un_hide()
break
if event == '确认转账':
to = values['-TO-']
amount = int(values['-AMOUNT-'])
if to in user_info:
if amount <= user_info[current_user]['balance']:
user_info[current_user]['balance'] -= amount
user_info[to]['balance'] += amount
sg.popup(f'转账成功,当前余额为{user_info[current_user]["balance"]}')
window_transfer.close()
window_main.un_hide()
else:
sg.popup('余额不足')
else:
sg.popup('该账号不存在')
```
这个ATM取款机项目使用了多个PySimpleGUI窗口,每个窗口对应一个功能,通过隐藏和显示窗口来实现不同功能之间的切换。在后端方面,该项目使用了一个字典来存储用户信息和账户余额,通过读写字典来实现各种操作。需要注意的是,这个项目只是一个简单示例,实际应用中还需要加入更多的安全性和容错处理。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)