2017-05-19 3 views
-1

私はPythonとTkinterを使って銀行システムのコードを書いています。私はプログラムを実行すると、私はいくつかのエラーが発生します。誰かがこのことを理解するのを助けてくれますか?私はそれについて研究しましたが、問題の解決策を見つけることはできません。ここでtkinter AttributeErrorが見つかりません

は誤りです:

...File "C:\Users\stjur\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2256, in _setup 
    self.tk = master.tk 
AttributeError: 'function' object has no attribute 'tk' 

そしてここでは私のコードです:

from tkinter import * 
root=Tk() 
root.title('Avis Banking System') 
def win1(): 
    global balance 
    balance=600 
    global root 
    global tries 
    root=Tk() 
    root.title('Avis banking system') 
    entry_1Var=StringVar() 
    entry_2Var=StringVar() 
    tries=0 

tries=0 
def CheckPass(): 
    global tries 

    if (tries<3): 
     textvalue=entry_1Var.get() 
     textvalue=entry_2Var.get() 
     if textvalue!=('John123') and textvalue!=('Secret'): 
      tries+=1 
      label3=Label(root,text='Try again') 
      label.grid(row=3,column=1,sticky=W,padx=7) 
       else: 
        win2() 
    if (tries==3): 
     label4=Label(root,text='Used all attempts, entry blocked') 
     label4.grid(row=3,column=1, sticky=W,padx=7) 

entry_1Var=StringVar() 
entry_2Var=StringVar() 
b1=Button(root,text='Login',width=5,command=CheckPass) 
b1.grid(row=1,column=10,sticky='e'+'w',padx=7) 
b2=Button(root,text='Reset',width=5,) 
b2.grid(row=2,column=10,sticky='e'+'w',padx=7) 
l1=Label(root,text='Username:').grid(row=1,padx=7,pady=5,sticky='we') 
l2=Label(root,text='Password:').grid(row=2,padx=7,pady=5,sticky='we') 
e1=Entry(root,width=30,textvariable=entry_1Var) 
e1.grid(row=1,column=1,sticky='e'+'w',columnspan=2) 
e2=Entry(root,show='*',width=30,textvariable=entry_2Var) 
e2.grid(row=2,column=1,sticky='e'+'w',columnspan=2) 
logLabel=Label(root,text='Login',font='bold',fg='Grey') 
logLabel.grid(row=0,padx=5,pady=5) 
check=Checkbutton(root,text='Keep me logged in') 
ckeck.grid(row=4,column=1,sticky=W,pady=5) 

if (tries==3): 
    win2 

def win2(): 
    #this is the child window of the main window. 
    win2=Toplevel(root) 
    win2.minsize(width=400,height=400) 
    win2.title("Main Menu") 
    #Label for the the main menu 
    lb=Label(win2,text='N&E Scotland Bank\nMain Menu:',bg='#e6e6e6',height=6).pack(fill=X) 

    btn_1=Button(win2,text='Make Deposit',width=15,height=2,command=win3).pack(pady=5) 
    btn_2=Button(win2,text='Withdrawal',width=15,height=2).pack(pady=5) 
    btn_3=Button(win2,text='Accounts',width=15,height=2).pack(pady=5) 
    btn_4=Button(win2,text='Balance',width=15,height=2).pack(pady=5) 
    btn_5=Button(win2,text='Exit',width=15,height=2).pack(pady=5) 

    def win3():#new window for the button make depost 
    win3=Tk() 
    win3.title('Make Deposit') 
    win3.minsize(width=400,height=120) 
    e1=IntVar() 
    e2=IntVar() 

def MakeDeposit(): 
    num2 = e2.get() 
    balance = int(num2) 
    num1 = e1.get() 
    depositAmount = int(num1) 
    while depositAmount<=0 or depositAmount%10!=0: 
     errorLabel = Label(win3,text="That was not a valid amount").grid(row=3,columnspan=5) 
     return# 
    textLabel = Label(win3,text=("Your new balance is:\n")).grid(row=3,columnspan=5) 
    newBalanceLabel = Label(win3,text=(balance+depositAmount)).grid(row=4,columnspan=5, pady=7) 

l1=Label(win3,text='Deposit Amount:').grid(row=1,padx=7,pady=5,sticky='we') 
l2=Label(win3,text='Balance:').grid(row=2,padx=7,pady=5,sticky='e') 
b1=Butto(win3,text='Calculate',width=8,height=3,command=MakeDeposit).grid(row=1,column=10,sticky='e'+'w',padx=7,rowspan=2) 
e1=Entry(win3,width=40) 
e1.grid(row=1,column=1,sticky='e'+'w',columnspan=3) 
e2=Entry(win3,width=40) 
e2.grid(row=2,column=1,sticky='e'+'w',columnspan=3) 
logLabel=Label(root,text='DEPOSIT',font='bold',fg='Grey').grid(row=0,padx=5,pady=5) 

root.mainloop() 
+0

コードに構文エラーがありません。 –

+0

あなたは 'Tk()'を複数の場所で呼び出しています。それはうまくいかないので、追加のウィンドウに 'Toplevel()'を使う必要があります。関数の名前とそれらの関数で作成されたウィンドウの名前の両方として 'win2'と' win3'を使用しています。それは動作しません。 – jasonharper

+0

'tkinter .__ init__'のエラー行2256は、内部BaseWidgetクラスのプライベート._setupメソッドにあります。それを解釈するには、元のコードを実行するときに上記のような完全なトレースバック(上記のようにコード化されたコード)を見る必要があります。 –

答えて

1

私はエラーメッセージを説明することができます。私のローカルコピーにフォーマットエラーを修正した後、私はこれを得た:

Traceback (most recent call last): 
    File "F:\Python\mypy\tem.py", line 84, in <module> 
    l1=Label(win3,text='Deposit Amount:').grid(row=1,padx=7,pady=5,sticky='we') 
    File "C:\Programs\Python36\lib\tkinter\__init__.py", line 2760, in __init__ 
    Widget.__init__(self, master, 'label', cnf, kw) 
    File "C:\Programs\Python36\lib\tkinter\__init__.py", line 2286, in __init__ 
    BaseWidget._setup(self, master, cnf) 
    File "C:\Programs\Python36\lib\tkinter\__init__.py", line 2256, in _setup 
    self.tk = master.tk 
AttributeError: 'function' object has no attribute 'tk' 

任意のウィジェットの作成呼び出しのための最初の、親の引数はウィジェットでなければなりません。 In

l1=Label(win3, ... 

属性を持たないwin3関数を渡しました。

将来的にはMCVEsをお読みください。

関連する問題