帮我用py写一个带图形的ATM机
时间: 2023-12-07 08:06:08 浏览: 62
基于MNIST数据集卷积神经网络实现手写数字识别(GUI)
5星 · 资源好评率100%
好的,我可以为您提供一些指导和代码示例。首先,您需要安装Python的图形用户界面库,如Tkinter或PyQt。然后,您可以按照以下步骤创建一个带有图形的ATM机:
1. 创建一个窗口,包括一个文本框(用于显示ATM操作的输出),一个标签(用于显示当前账户余额)和一些按钮(例如,取款、存款、转账等)。
2. 定义一个ATM类,包括以下方法:
- check_balance():检查账户余额并将其显示在标签中。
- withdraw(amount):从账户中取出一定金额,更新余额并将结果显示在文本框中。
- deposit(amount):将一定金额存入账户中,更新余额并将结果显示在文本框中。
- transfer(amount, account):将一定金额转入另一个账户中,更新余额并将结果显示在文本框中。
3. 将ATM类与按钮关联,使用户可以通过单击按钮来执行相应的操作。
以下是一个简单的示例代码:
```
import tkinter as tk
class ATM:
def __init__(self, balance):
self.balance = balance
def check_balance(self):
balance_label.config(text="Your balance is $" + str(self.balance))
def withdraw(self, amount):
if amount > self.balance:
result_text.config(text="Insufficient funds")
else:
self.balance -= amount
result_text.config(text="Withdrawal successful. Your new balance is $" + str(self.balance))
self.check_balance()
def deposit(self, amount):
self.balance += amount
result_text.config(text="Deposit successful. Your new balance is $" + str(self.balance))
self.check_balance()
def transfer(self, amount, account):
if amount > self.balance:
result_text.config(text="Insufficient funds")
else:
self.balance -= amount
account.balance += amount
result_text.config(text="Transfer successful. Your new balance is $" + str(self.balance))
self.check_balance()
atm = ATM(1000)
def check_balance():
atm.check_balance()
def withdraw():
amount = int(amount_entry.get())
atm.withdraw(amount)
def deposit():
amount = int(amount_entry.get())
atm.deposit(amount)
def transfer():
amount = int(amount_entry.get())
account = ATM(500)
atm.transfer(amount, account)
window = tk.Tk()
window.title("ATM")
window.geometry('400x300')
balance_label = tk.Label(window, text="")
balance_label.pack()
result_text = tk.Label(window, text="")
result_text.pack()
check_balance_button = tk.Button(window, text="Check balance", command=check_balance)
check_balance_button.pack()
amount_label = tk.Label(window, text="Amount:")
amount_label.pack()
amount_entry = tk.Entry(window)
amount_entry.pack()
withdraw_button = tk.Button(window, text="Withdraw", command=withdraw)
withdraw_button.pack()
deposit_button = tk.Button(window, text="Deposit", command=deposit)
deposit_button.pack()
transfer_button = tk.Button(window, text="Transfer", command=transfer)
transfer_button.pack()
window.mainloop()
```
请注意,这只是一个简单的示例代码,您可以根据自己的需求进行修改和调整。
阅读全文