import tkinter as tk from tkinter import messagebox from PIL import Image, ImageTk import csv class BankSystem: def __init__(self): self.accounts = {} self.current_id = 0 self.load_from_csv() def create_account(self, name, amount): if name in self.accounts: messagebox.showerror('错误', '帐户已存在.') else: self.current_id += 1 self.accounts[name] = [self.current_id, float(amount)] self.save_to_csv() def deposit(self, name, amount): if name not in self.accounts: messagebox.showerror('错误', '找不到帐户.') else: self.accounts[name][1] += float(amount) self.save_to_csv() messagebox.showinfo('成功', f'{amount} 元人民币存入帐户 {name}') def withdraw(self, name, amount): if name not in self.accounts: messagebox.showerror('错误', '找不到帐户..') else: if self.accounts[name][1] < float(amount): messagebox.showerror('错误', '余额不足') else: self.accounts[name][1] -= float(amount) self.save_to_csv() messagebox.showinfo('成功', f'{amount} 元人民币从帐户中取出 {name}') def transfer(self, sender_name, receiver_name, amount): if sender_name not in self.accounts or receiver_name not in self.accounts: messagebox.showerror('错误', '账号不存在') else: if self.accounts[sender_name][1] < float(amount): messagebox.showerror('错误', '余额不足') else: self.accounts[sender_name][1] -= float(amount) self.accounts[receiver_name][1] += float(amount) self.save_to_csv() messagebox.showinfo('成功', f'{amount} 元人民币从帐户 {sender_name} 转出到帐户 {receiver_name}') def check_balance(self, name): if name not in self.accounts: messagebox.showerror('错误', '找不到帐户..') else: balance = self.accounts[name][1] balance = int(balance) messagebox.showinfo('Balance', f'账户 {name} 余额为 {balance} 元') def save_to_csv(self): with open('账户信息.csv', mode='w', newline='') as file: writer = csv.writer(file) writer.writerow(['账户名', '账户ID', '余额']) for name, data in self.accounts.items(): writer.writerow([name, data[0], data[1]]) def load_from_csv(self): try: with open('账户信息.csv', mode='r') as file: reader = csv.reader(file) next(reader) for row in reader: name, account_id, balance = row self.accounts[name] = [int(account_id), float(balance)] self.current_id = max(self.current_id, int(account_id)) except FileNotFoundError: pass class BankGUI: def __init__(self): self.bank = BankSystem() self.root = tk.Tk() self.root.title('银行管理系统') self.root.geometry('900x800') # 设置背景图片 self.background_image = Image.open('background.jpg') self.background_photo = ImageTk.PhotoImage(self.background_image) self.background_label = tk.Label(self.root, image=self.background_photo) self.background_label.place(x=0, y=0, relwidth=1, relheight=1) # 创建一个主框架来放置所有组件 self.main_frame = tk.Frame(self.root, bg='lightblue', padx=10, pady=10) self.main_frame.place(relx=0.5, rely=0.5, anchor=tk.CENTER) # 创建账户 self.create_label = tk.Label(self.main_frame, text='创建账户', font=('Arial', 16, 'bold'), bg='lightblue') self.create_label.grid(row=0, column=0, columnspan=2, pady=10) self.name_label = tk.Label(self.main_frame, text='账户名:', bg='lightblue') self.name_label.grid(row=1, column=0, pady=5) self.name_entry = tk.Entry(self.main_frame) self.name_entry.grid(row=1, column=1, pady=5) self.amount_label = tk.Label(self.main_frame, text='初始金额:', bg='lightblue') self.amount_label.grid(row=2, column=0, pady=5) self.amount_entry = tk.Entry(self.main_frame) self.amount_entry.grid(row=2, column=1, pady=5) self.create_button = tk.Button(self.main_frame, text='创建', command=self.create_account, bg='green', fg='white') self.create_button.grid(row=3, column=1, pady=10) # 存款 self.deposit_label = tk.Label(self.main_frame, text='存款', font=('Arial', 16, 'bold'), bg='lightblue') self.deposit_label.grid(row=4, column=0, columnspan=2, pady=10) self.deposit_name_label = tk.Label(self.main_frame, text='账户名:', bg='lightblue') self.deposit_name_label.grid(row=5, column=0, pady=5) self.deposit_name_entry = tk.Entry(self.main_frame) self.deposit_name_entry.grid(row=5, column=1, pady=5) self.deposit_amount_label = tk.Label(self.main_frame, text='存款金额:', bg='lightblue') self.deposit_amount_label.grid(row=6, column=0, pady=5) self.deposit_amount_entry = tk.Entry(self.main_frame) self.deposit_amount_entry.grid(row=6, column=1, pady=5) self.deposit_button = tk.Button(self.main_frame, text='存款', command=self.deposit, bg='green', fg='white') self.deposit_button.grid(row=7, column=1, pady=10) # 取款 self.withdraw_label = tk.Label(self.main_frame, text='取款', font=('Arial', 16, 'bold'), bg='lightblue') self.withdraw_label.grid(row=8, column=0, columnspan=2, pady=10) self.withdraw_name_label = tk.Label(self.main_frame, text='账户名:', bg='lightblue') self.withdraw_name_label.grid(row=9, column=0, pady=5) self.withdraw_name_entry = tk.Entry(self.main_frame) self.withdraw_name_entry.grid(row=9, column=1, pady=5) self.withdraw_amount_label = tk.Label(self.main_frame, text='取款金额:', bg='lightblue') self.withdraw_amount_label.grid(row=10, column=0, pady=5) self.withdraw_amount_entry = tk.Entry(self.main_frame) self.withdraw_amount_entry.grid(row=10, column=1, pady=5) self.withdraw_button = tk.Button(self.main_frame, text='取款', command=self.withdraw, bg='green', fg='white') self.withdraw_button.grid(row=11, column=1, pady=10) # 转账 self.transfer_label = tk.Label(self.main_frame, text='转账', font=('Arial', 16, 'bold'), bg='lightblue') self.transfer_label.grid(row=12, column=0, columnspan=2, pady=10) self.transfer_sender_label = tk.Label(self.main_frame, text='转出账户:', bg='lightblue') self.transfer_sender_label.grid(row=13, column=0, pady=5) self.transfer_sender_entry = tk.Entry(self.main_frame) self.transfer_sender_entry.grid(row=13, column=1, pady=5) self.transfer_receiver_label = tk.Label(self.main_frame, text='转入账户:', bg='lightblue') self.transfer_receiver_label.grid(row=14, column=0, pady=5) self.transfer_receiver_entry = tk.Entry(self.main_frame) self.transfer_receiver_entry.grid(row=14, column=1, pady=5) self.transfer_amount_label = tk.Label(self.main_frame, text='转账金额:', bg='lightblue') self.transfer_amount_label.grid(row=15, column=0, pady=5) self.transfer_amount_entry = tk.Entry(self.main_frame) self.transfer_amount_entry.grid(row=15, column=1, pady=5) self.transfer_button = tk.Button(self.main_frame, text='转账', command=self.transfer, bg='green', fg='white') self.transfer_button.grid(row=16, column=1, pady=10) # 查询余额 self.balance_label = tk.Label(self.main_frame, text='查询余额', font=('Arial', 16, 'bold'), bg='lightblue') self.balance_label.grid(row=17, column=0, columnspan=2, pady=10) self.balance_name_label = tk.Label(self.main_frame, text='账户名:', bg='lightblue') self.balance_name_label.grid(row=18, column=0, pady=5) self.balance_name_entry = tk.Entry(self.main_frame) self.balance_name_entry.grid(row=18, column=1, pady=5) self.balance_button = tk.Button(self.main_frame, text='查询', command=self.check_balance, bg='green', fg='white') self.balance_button.grid(row=19, column=1, pady=10) self.root.mainloop() def create_account(self): name = self.name_entry.get() amount = self.amount_entry.get() self.bank.create_account(name, amount) messagebox.showinfo('Success', f'创建成功 账号:{name} 账号余额:{amount}') def deposit(self): name = self.deposit_name_entry.get() amount = self.deposit_amount_entry.get() self.bank.deposit(name, amount) def withdraw(self): name = self.withdraw_name_entry.get() amount = self.withdraw_amount_entry.get() self.bank.withdraw(name, amount) def transfer(self): sender = self.transfer_sender_entry.get() receiver = self.transfer_receiver_entry.get() amount = self.transfer_amount_entry.get() self.bank.transfer(sender, receiver, amount) def check_balance(self): name = self.balance_name_entry.get() self.bank.check_balance(name) if __name__ == '__main__': bank_gui = BankGUI()