Tkinter框架下函数内If-Else语句失效问题求解
Hey there! I see you've been stuck on this for 5 hours—let's get this sorted out quickly.
The Root Cause
The problem in your detail() function is that you're comparing the Entry widget object itself (en) to the string "91xxxxxxxx", not the text the user typed into the entry box.
In Tkinter, en is an instance of the Entry class, not the input value. So when you run if en == "91xxxxxxxx":, you're checking if a widget equals a string—this will never be true, which is why your label never shows up.
The Solution
You need to use the .get() method on the Entry widget to retrieve the actual text the user entered. Here's how to fix your detail() function:
def detail(): # Get the text from the Entry widget account_number = en.get() if account_number == "91xxxxxxxx": Label(text="Hello").pack() # Optional: Add an else case to handle incorrect inputs else: tmsg.showinfo("Oops!", "Invalid account number.")
Bonus: Preventing "Undefined Variable" Errors
Another thing to watch out for: if someone clicks the "View my Account Details" button before clicking "View My Account" (which creates the en Entry), your code will throw an error because en doesn't exist yet. You can add a check to handle this:
def detail(): # Check if the Entry widget exists first try: account_number = en.get() if account_number == "91xxxxxxxx": Label(text="Hello").pack() else: tmsg.showinfo("Oops!", "Invalid account number.") except NameError: tmsg.showwarning("Wait!", "Please enter your account number first.")
Full Modified Code
Here's your full code with the fixes applied (plus a tiny typo fix in the transaction button text):
from tkinter import * import tkinter.messagebox as tmsg root = Tk() def myaccount(): Label(root, text="\n\nEnter Your Account Number : ").pack(anchor="nw") global en en = Entry(font="helectiva 11") en.pack(anchor="nw") def detail(): try: account_number = en.get() if account_number == "91xxxxxxxx": Label(text="Hello").pack() else: tmsg.showinfo("Oops!", "Invalid account number.") except NameError: tmsg.showwarning("Wait!", "Please enter your account number first.") b = Button(root, text="View my Account Details",borderwidth=10,command=detail) b.pack(anchor="nw") txt = StringVar() txt.set("——Welcome To Banking System Application——") txt1 = StringVar() txt1.set("*****************************************************") txt2 = StringVar() txt2.set("\n\nChoose from the Options :") txt3 = StringVar() txt3.set("—————————————————————————————") Label(root, textvariable = txt).pack() Label(root, textvariable = txt1).pack() Label(root, textvariable = txt2).pack() Label(root, textvariable= txt3).pack() Button(root, text="View My Account",borderwidth=10,command=myaccount).pack(anchor="nw",ipadx=32) Button(root, text="New Account",borderwidth=10).pack(anchor="nw",ipadx=72) Button(root, text="Make a Transaction",borderwidth=10).pack(anchor="nw") Button(root, text="Exit",borderwidth=10,command=quit).pack(anchor="nw",ipadx=170) root.mainloop()
Quick Tip for Better Code
Using global variables can get messy as your app grows. For larger Tkinter projects, consider using a class to encapsulate your widgets and functions—this makes your code easier to maintain and avoids global variable headaches.
内容的提问来源于stack exchange,提问作者Atul Singh

