无法找到tkinter AttributeError

时间:2017-05-19 23:47:36

标签: python tkinter

我正在使用python和Tkinter编写银行系统代码。当我运行程序时,我遇到了一些错误。请有人帮我解决这个问题。我已对此进行了研究,但无法找到问题的解决方案。

这是错误:

 ...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()

1 个答案:

答案 0 :(得分: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'

任何Widget创建调用的第一个parent参数必须是一个小部件。在

l1=Label(win3, ...

您通过了win3函数,该函数没有tk属性。

将来,请阅读MCVEs