Home > Mobile >  If-Else not working in Tkinter under functions please solve this question :
If-Else not working in Tkinter under functions please solve this question :

Time:10-30

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():
        if en=="91xxxxxxxx":
                Label(text="Hello").pack()
    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 Transactiom",borderwidth=10).pack(anchor="nw")
Button(root, text="Exit",borderwidth=10,command=quit).pack(anchor="nw",ipadx=170)

root.mainloop()

Please help me with this . The if else in mydetails is not working . I tried to solve it for 5 hours straight but couldn't find the correct reason . But if you find the reason please give the answer , I will be very thankful to that person.

CodePudding user response:

An entry object cannot be a string. Instead, you must get the text inside the entry object:

if en.get() == '91xxxxxxxx'
    Label(text="Hello").pack()

Final code:

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():
        if en.get() == '91xxxxxxxx'
            Label(text="Hello").pack()
    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 Transactiom",borderwidth=10).pack(anchor="nw")
Button(root, text="Exit",borderwidth=10,command=quit).pack(anchor="nw",ipadx=170)

root.mainloop()
  • Related