2013-04-15 105 views
0

我有BankAccount類,我正在使用它來創建一個允許用戶進行存款,進行提款並查看餘額的GUI。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' 

回答

3

當您在deposit函數中調用balance= BankAccount.getBalance(self),你實際上做的是訪問BankAccount類的getBalance()方法,使用它初始化,並試圖在不同的對象傳遞爲self。當您通過訪問類而不是實例來調用某個方法時,您必須給它一個self對象才能真正起作用。 BankAccount方法期望它們的self對象成爲BankAccount對象。您將它傳遞給BankAccountGUI對象,而不包含__balance屬性。這就是爲什麼它拋出這個錯誤。

你應該做的是創造的BankAccount的一個實例,然後使用它的方法:

account = BankAccount() 
balance = account.getBalance() 

類似的東西。

0

要理解錯誤消息的變量_BankAccount__balance的提及, 看到the Python documentation上使用雙下劃線和「名稱重整」:

...由於存在有效使用對於類私有成員(即爲了避免名稱與由子類定義的名稱發生名稱衝突)而言,對這種稱爲名稱修改的機制的支持有限。任何標識符__spam(至少包含兩個前導下劃線,最多一個尾部下劃線)的文本格式將被替換爲_classname__spam,其中,classname是當前類名稱,前導下劃線已剝離。

This question及其接受的答案也是信息性的。

解決這個問題的最簡單方法就是從您的類變量名稱中刪除所有前導下劃線。或者你可以將self.__balance中的BankAccount.getBalance()函數更改爲self._BankAccount__balance

編輯:您還將bankAccountGUI對象作爲getBalance函數中的參數傳遞,如self。它應該是balance= BankAccount.getBalance(),沒有self

+0

我已經解決了這個問題,我只是想知道,如果你知道爲什麼一個消息框,簡單地說「警報」彈出時,我打入押金入境輸入。我一直無法找到任何有關這意味着什麼的信息,或者是什麼原因造成的。 – tinydancer9454

+0

Junuxx,它不是關於雙下劃線,它實際上是類和類的實例之間的混淆。 GUI對象應該創建一個BankAccount實例,並且BankAccountGUI中的所有方法都應該從該實例運行,而不是脫離該類對象本身。 – DaveTheScientist

+0

@tinydancer:只是「警覺」確實有點無益。你有沒有設法找出造成它的原因? – Junuxx