下面是一个简单的银行账户,我使用Tkinter在Python中编码,问题是在撤销和存款函数中使用条件语句,代码总是与else语句一起使用,尽管在标准和兴趣函数中它应该更改TypeOfAccount的值。提供你的见解。
import tkinter
from tkinter import *
from random import randint
class BankAccount(object):
def __init__(self, initial_balance=0):
self.balance = initial_balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
def get_balance(self, initial_balance, rate):
return self.get_balance() * self._rate
class BankAccountWithInterest(BankAccount):
def __init__(self, initial_balance=0, rate=0.1):
BankAccount.__init__(self, initial_balance)
self._rate = rate
def interest(self):
return self.balance * self._rate
balance = (randint(100, 500))
my_account = BankAccount(balance)
my_interest = BankAccountWithInterest(balance)
interest = my_interest.balance + my_interest.interest()
print(interest)
class GUI:
def __init__(self, master):
frame = Frame(master)
frame.pack()
#Toolbar#
toolbar = Frame(root)
toolbar.pack(side=TOP, fill=X)
#Button#
button1 = Button(toolbar, text="Deposit", width = 13, command=self.depositBalance)
button2 = Button(toolbar, text="Withdraw",width = 13, command=self.depositWithdraw)
button1.pack(side=LEFT)
button2.pack(side=RIGHT)
#Menu#
subMenu = Menu(menu)
menu.add_cascade(label="Type of Account", menu=subMenu)
subMenu.add_command(label="Standard", command= self.standard)
subMenu.add_command(label="Interest", command= self.interest)
#Textbox#
self.text = Entry(root)
self.text.pack()
def standard(self):
typeOfAcc = "standard"
w1.config(text=my_account.balance)
w1.pack()
def interest(self):
typeOfAccount = "interest"
w1.config(text=interest)
w1.pack()
def depositBalance(self):
if typeOfAccount == "interest":
a = int(self.text.get())
interest = interest + a
w1.config(text=interest)
elif typeOfAccount == "standard":
a = int(self.text.get())
my_account.balance = my_account.balance + a
print(my_account.balance)
w1.config(text=my_account.balance)
else:
w1.config(text="Error: Select account type")
def depositWithdraw(self):
if typeOfAccount == 1:
a = int(self.text.get())
interest = interest - a
w1.config(text=interest)
elif typeOfAccount == 0:
a = int(self.text.get())
my_account.balance = my_account.balance - a
print(my_account.balance)
w1.config(text=my_account.balance)
else:
w1.config(text="Error: Select account type")
typeOfAccount = 0
root = Tk()
menu = Menu(root)
root.config(menu=menu)
root.title("Bank Account")
root.minsize(width=250, height=100)
root.maxsize(width=300, height=150)
#Labels#
w = Label(root, text="Current Balance:")
w.pack()
w1 = Label(root, text="0")
w1.pack()
GUI(root)
root.mainloop()
答案 0 :(得分:1)
部分问题在于,在您的deposit
函数中,您正在检查typeOfAccount
是'standard'
还是'interest'
,而在withdraw
函数中,您正在检查typeOfAccount
是1
还是0
。这种不一致将导致错误和意外行为。
我还强烈建议您放置这两个代码块:
balance = (randint(100, 500))
my_account = BankAccount(balance)
my_interest = BankAccountWithInterest(balance)
interest = my_interest.balance + my_interest.interest()
print(interest)
typeOfAccount = 0
root = Tk()
menu = Menu(root)
root.config(menu=menu)
root.title("Bank Account")
root.minsize(width=250, height=100)
root.maxsize(width=300, height=150)
#Labels#
w = Label(root, text="Current Balance:")
w.pack()
w1 = Label(root, text="0")
w1.pack()
在您的主GUI
课程中。
class Account:
def __init__(self, init_balance=0):
self.balance = init_balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
def get_balance(self, init_balance, rate):
return self.get_balance() * self._rate
class InterestAccount(Account):
def __init__(self, init_balance=0, rate=0.1):
super().__init__(init_balance)
self._rate = rate
def interest(self):
return self.balance * self._rate
class GUI(Tk):
def __init__(self):
Tk.__init__(self)
self.title('Bank Account')
#Menu#
menu = Menu(self)
acct_type_menu = Menu(menu)
menu.add_cascade(label='Account Type', menu=acct_type_menu)
acct_type_menu.add_command(label='Standard', command=self.set_type_standard)
acct_type_menu.add_command(label='Interest', command=self.set_type_interest)
self.config(menu=menu)
#Account#
start_balance = randint(100, 500)
self.acct = Account(start_balance)
self.my_interest = InterestAccount(start_balance)
self.interest = self.my_interest.balance + self.my_interest.interest()
#Labels#
Label(self, text='Current Balance:').pack()
self.balance_label = Label(self, text='Error: Select account type')
self.balance_label.pack()
#Button#
btns_frame = Frame(self)
btns_frame.pack(side=TOP, fill=X)
Button(btns_frame, text='Deposit', width=13, command=self.deposit).pack(side=LEFT)
Button(btns_frame, text='Withdraw', width=13, command=self.withdraw).pack(side=RIGHT)
#Textbox#
self.text = Entry(self)
self.text.pack()
def set_type_standard(self):
self.acct_type = 'standard'
self.balance_label.config(text=self.acct.balance)
def set_type_interest(self):
self.acct_type = 'interest'
self.balance_label.config(text=self.interest)
def clear_entry(self):
self.text.delete(0, END)
def deposit(self):
if self.acct_type == 'interest':
a = int(self.text.get())
interest = interest + a
self.balance_label.config(text=self.interest)
elif self.acct_type == 'standard':
a = int(self.text.get())
self.acct.balance += a
self.balance_label.config(text=self.acct.balance)
else:
self.balance_label.config(text='Error: Select account type')
self.clear_entry()
def withdraw(self):
if self.acct_type == 'interest':
a = int(self.text.get())
self.interest -= a
self.balance_label.config(text=self.interest)
elif self.acct_type == 'standard':
a = int(self.text.get())
self.acct.balance -= a
self.balance_label.config(text=self.acct.balance)
else:
self.balance_label.config(text='Error: Select account type')
self.clear_entry()
if __name__ == '__main__':
GUI().mainloop()
GUI
继承自Tk
GUI
类deposit
和withdraw
方法以使用deposit
类withdraw
和Account
Error: Select account type
Deposit
或Withdraw
按钮时的明确输入编辑:由于您使用的Entry
小部件用于存款和提款金额,因此您可能希望将Entry
的输入限制为仅限数字和期限:
class GUI:
def __init__(self):
...
vcmd = (self.register(self.onValidate), '%S')
self.text = Entry(self, validate='key', vcmd=vcmd)
self.text.pack()
...
def onValidate(self, S):
if S in '0123456789.':
return True
return False
...