0

Im getting error when am trying to access the function name "def check" in which i've stated some if else cnditions which is not working as expected, any help will be highly appreciated.

HERES THE CODE

from tkinter import *
w=Tk()
w.geometry('1000x800')
w.configure(bg='#6863f6')
def h1():
    otpentry = Entry(w, textvar=StringVar(),bg="white", fg="black")
    otpentry.place(x=290, y=330)
    optbuton = Button(w, text="subbmit",bg='white', fg='black',command=check)
    optbuton.place(x=420, y=330)

def check():
    if int(otpentry.get())==1234:
        timeLabl = Label(w, text=" Bank Transaction Successful", bg="white", fg="black")
        timeLabl.place(x=250, y=400)
    else:
        int(otpentry.get())!=1234:
        timeLabl = Label(w, text=" Bank Transaction Failed", bg="white", fg="black")
        timeLabl.place(x=250, y=400)
bu1=Button(w,text="ok",bg='white',fg='black',command=h1)
bu1.place(x=200,y=180)
w.mainloop()
3
  • 1
    what is the error ? what is your expectation ? Commented Sep 3, 2020 at 11:00
  • I want that if user type 1234 in entry box then it will get printe “Bank Transaction Successful” and if user type other than 1234 then it will get printed “ Bank Transaction Failed” Commented Sep 3, 2020 at 11:04
  • Try to print otpentry.get() inside check() to see if the value is as you expected Commented Sep 3, 2020 at 11:14

3 Answers 3

1

Here you go, ive fixed this code out. The problem was quite simple, you are defining the Entry() inside of function and hence, it stays within the function, to make it accessible to other functions, you have to use global.

Here is the working version of your code:

from tkinter import *

w=Tk()
w.geometry('1000x800')
w.configure(bg='#6863f6')
def h1():
    global otpentry #so its available to all scope
    passw = StringVar() #proper way to define and pass in a tkinter var
    otpentry = Entry(w, textvar=passw,bg="white", fg="black") 
    otpentry.place(x=290, y=330)
    optbuton = Button(w, text="subbmit",bg='white', fg='black',command=check)
    optbuton.place(x=420, y=330)

def check():
    if int(otpentry.get())==1234: #Also can use passw.get().
        timeLabl = Label(w, text=" Bank Transaction Successful", bg="white", fg="black")
        timeLabl.place(x=250, y=400)
    else:
        timeLabl = Label(w, text=" Bank Transaction Failed", bg="white", fg="black")
        timeLabl.place(x=250, y=400)
bu1=Button(w,text="ok",bg='white',fg='black',command=h1)
bu1.place(x=200,y=180)
w.mainloop()

Ive also done the proper way of defining a tkinter variable like StringVar() or IntVar() too.

Ive removed this syntactically wrong line:

else:
    int(otpentry.get())!=1234:
....

TIP

To avoid the label from overwriting you can define the label outside all the functions like

timeLabl = Label(w,bg="white", fg="black")

and then change your function to:

def check():
    if int(otpentry.get())==1234: #Also can use passw.get() and get rid of typecasting
        timeLabl.config(text=" Bank Transaction Successful")
        timeLabl.place(x=250, y=400)
    else:
        timeLabl.config(text="Bank Transaction Failed")
        timeLabl.place(x=250, y=400)

The config() method will just edit the text of the label always and will prevent it from overwriting.

Hope you understood your mistakes, do let me know if any errors.

Cheers

Sign up to request clarification or add additional context in comments.

2 Comments

It worked but am getting 0 already entered in the entry box
@LemonBitter change IntVar() to StringVar(), if it helped make sure to mark as the answer :D
0

Minimal changes to achieve that:

from tkinter import *
from functools import partial

w=Tk()
w.geometry('1000x800')
w.configure(bg='#6863f6')

timeLabl = None

def h1():
    otpentry = Entry(w, textvar=StringVar(), bg="white", fg="black")
    otpentry.place(x=290, y=330)
    optbuton = Button(w, text="Submit", bg='white', fg='black', command=partial(check, otpentry))
    optbuton.place(x=420, y=330)

def check(otpentry):
    global timeLabl
    if timeLabl:
        timeLabl.place_forget()
    if int(otpentry.get()) == 1234:
        timeLabl = Label(w, text=" Bank Transaction Successful", bg="white", fg="black")
        timeLabl.place(x=250, y=400)
    else:
        timeLabl = Label(w, text=" Bank Transaction Failed", bg="white", fg="black")
        timeLabl.place(x=250, y=400)

bu1 = Button(w, text="Ok", bg='white', fg='black', command=h1)
bu1.place(x=200,y=180)
w.mainloop()

Things that changed:

  • Using partial to pass argument to command instead of having global variable to fix error in check subroutine (undefined otpentry)
  • Making label disappear with place_forget to avoid showing overlapping labels

5 Comments

Please show/explain/describe what you did instead of just pasting code
If condition fails then why both the labels are appearing? @Roxy
I followed ur code but it’s showing error “partial is not defined”
I want that if user type 1234 in entry box then it will get printe “Bank Transaction Successful” and if user type other than 1234 then it will get printed “ Bank Transaction Failed
Karthik I've just fixed that label overlapping, nordmanden added a description of what changed, Lemon Bitter you need to import the library as in the code snippet
0

try this code,

from tkinter import *
w=Tk()
w.geometry('1000x800')
w.configure(bg='#6863f6')
otpentry = Entry(w, textvar=StringVar(),bg="white", fg="black")
otpentry.place(x=290, y=330)
timeLabl = Label(w, bg="#6863f6", fg="black")
timeLabl.place(x=250, y=400)
def h1():
    optbuton = Button(w, text="subbmit",bg='white', fg='black',command=check)
    optbuton.place(x=420, y=330)

def check():
    if int(otpentry.get())==1234:
        timeLabl.config(text=" Bank Transaction Successful",bg='white')
    else: # changes made here
        timeLabl.config(text=" Bank Transaction Failed",bg='white')
bu1=Button(w,text="ok",bg='white',fg='black',command=h1)
bu1.place(x=200,y=180)
w.mainloop()

i edited your code

  1. Declared Entry out side the function, so it can be accessed in any function
  2. Same as Label also, (made label same as background color)
  3. Used 'config()' to make changes as button clicked

1 Comment

The reason why OP had Entry inside a function was to show it when the button was pressed, i guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.