简体   繁体   中英

Python, tkinter Pop up Window Errors

I made a code to simulate an ATM interface, however the 2nd phase seems to have a bug. Step1: Ask to create/choose a bank Account Step2: Pick "create" it goes to def create account : which opens Step3: Enter account number: error(Variable used seems to be undefined?) I dont see the problem, maybe Im blind but i dont see what could be causing the error. Why is my variable: userAnswer always come back undefined.

import tkinter

x=''
bankList = ['100','101','102','103','104','105','106','107','108','109']



def checkAccount():
    number = userAnswer.get()
    if number == '1':#in bankList:
        print("That Account already exist, try another number.")
    else:
        bankList.append(number)
        print("Your new account has been created!")

def createAccount():
    window2 = tkinter.Tk()
    window2.title("Creating an Account!")
    window2.geometry("400x100")

    accountLabel = tkinter.Label(window2, text="Please input the 3 digit number for the Account: ")
    userAnswer = tkinter.Entry(window2)
    accountButton = tkinter.Button(window2, text="Go", command=checkAccount)

    accountLabel.pack()
    userAnswer.pack()
    accountButton.pack()


def selectAccount():
    print("nope")


#------------------------- Opening Text Box: Create / Choose Account
window = tkinter.Tk()
window.title("ATM - Inovated Online Banking")
window.geometry("400x100")

label = tkinter.Label(window, text="Thank you for using online Banking Canada. Howe can we help you?")
button = tkinter.Button(window, text="Create Account", command=createAccount)
button2 = tkinter.Button(window, text="Select Account",   command=selectAccount) 

label.pack()
button.pack()
button2.pack()

Look at this line: number = userAnswer.get()

This is local to the createAccount function. You could re-structure, pass it in as an argument, or various other ways.

Also you should never have two instances of tk.Tk() . So, you should restructure. If you really want a new window you could just use tk.Toplevel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM