У меня есть класс BankAccount, который я использую для создания графического интерфейса пользователя, который позволяет пользователю делать депозит, делать вывод и видеть их баланс.ошибка атрибута tkinter
Это код класса BankAccount:
class BankAccount(object):
""" creates a bank account with the
owner's name and a balance """
def __init__(self, name, balance = 0):
self.__name = name
self.__balance = balance
def getName(self):
""" returns the owner's name """
return self.__name
def getBalance(self):
""" returns the current balance """
return round(self.__balance, 2)
def deposit(self, amount):
""" deposits amount into the account """
self.__balance += amount
def withdraw(self, amount):
""" withdraws amount from the account
returns 'overdrawn' if balance is too low """
if self.__balance >= amount:
self.__balance -= amount
else:
return 'overdrawn'
def __str__(self):
""" return a string representation of the account """
return self.__name + ' has a balance of $' + str(round(self.__balance, 2))
И это код GUI:
from tkinter import *
from bankAccountClass import BankAccount
class bankAccountGUI(Frame):
def __init__(self):
"""Set up the GUI"""
self.__balance= 0
Frame.__init__(self)
self.master.title('Bank Account')
self.grid()
depositLabel = Label(self, text= "Make Deposit")
depositLabel.grid(row = 0, column = 0)
self.depositVar= DoubleVar()
depositEntry = Entry(self, textvariable= self.depositVar)
depositEntry.grid(row = 0, column = 1)
withdrawLabel= Label(self, text= "Make Withdrawal")
withdrawLabel.grid(row = 1, column = 0)
self.withdrawVar = DoubleVar()
withdrawEntry= Entry(self, textvariable= self.withdrawVar)
withdrawEntry.grid(row = 1, column = 1)
button_1= Button(self, text = "Enter", command = self.deposit)
button_1.grid(row = 0, column = 2)
button_2= Button(self, text = "Enter", command = self.withdrawal)
button_2.grid(row = 1, column = 2)
def deposit(self):
"""event handler for button_1"""
try:
amount= self.depositVar.get()
balance= BankAccount.getBalance(self)
if amount <= 0:
messagebox.showerror(message= 'Deposit must be greater than 0')
else:
balance= BankAccount.deposit(self, amount)
messagebox.showinfo(title= "Current Balance",
message= "$" + self.balance,
parent= self)
except ValueError:
messagebox.showerror(message= "Invalid deposit amount")
def withdrawal(self):
"""event handler for button_2"""
try:
amount= self.withdrawVar.get()
balance= BankAccount.getBalance(self)
if amount > self.balance:
messagebox.showerror(message= "Insufficient funds")
else:
balance= BankAccount.withdraw(self, amount)
messagebox.showinfo(title= "Current Balance",
message= "$" + self.balance,
parent= self)
except ValueError:
messagebox.showerror(message= "Invalid withdrawal amount")
def main():
"""instantiate and pop up the window"""
bankAccountGUI().mainloop()
Я получаю сообщение об ошибке, что я не знаю, что это значит, и как Исправить это.
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 1442, in __call__
return self.func(*args)
File "/Users/tinydancer9454/Documents/python/bankAccountGUI.py", line 49, in deposit
balance= BankAccount.getBalance(self)
File "/Users/tinydancer9454/Documents/python/bankAccountClass.py", line 24, in getBalance
return round(self.__balance, 2)
AttributeError: 'bankAccountGUI' object has no attribute '_BankAccount__balance'
Я исправил это, и мне было просто интересно, знаете ли вы, почему появляется окно сообщения, в котором просто говорится «предупреждение», когда я нажимаю enter для ввода вклада. Я не смог найти какую-либо информацию о том, что это означает или что вызывает его. – tinydancer9454
Junuxx, речь идет не о двойных подчеркиваниях, а о путанице между классами и экземплярами классов. Объект GUI должен создать экземпляр BankAccount, и все методы в BankAccountGUI должны работать от этого экземпляра, а не от самого объекта класса. – DaveTheScientist
@tinydancer: Просто «оповещение» действительно немного бесполезно.Вам удалось выяснить, что вызвало это? – Junuxx