I have a task to make PIN panel in GUI which is supposed to check if PIN is correct and give a message "Correct PIN" or "Wrong PIN". I managed to make the whole code, but it returns me "Wrong PIN" message even if I write a correct PIN.
I tried to change type of PIN in string and integer, change a place where I call the function check_pin(), but I get always the same (wrong) input
Code:
from tkinter import *
root = Tk()
root.geometry ('800x300')
root.title('Check PIN')
number1_var = StringVar()
def button_add(number):
number1 = entry_PIN.get()
entry_PIN.delete (0, END)
entry_PIN.insert (0, number1+str(number))
pin = '1234'
def check_PIN():
number1=number1_var.get()
if number1 == pin:
label1 = Label (root, text='Correct PIN!')
label1.grid(row=6, column=1)
else:
label2 = Label (root, text='Wrong PIN!')
label2.grid(row=7, column=1)
label_pin = Label (root, text='PIN:')
label_pin.grid(row=0, column=0, padx=10, pady=10, sticky='e')
entry_PIN = Entry(root)
entry_PIN.grid (row=0, column=1, padx=10, pady=10)
button1 = Button(root, text='1', width=7, command=lambda: button_add(1))
button2 = Button(root, text='2', width=7, command=lambda: button_add(2))
button3 = Button(root, text='3', width=7, command=lambda: button_add(3))
button4 = Button(root, text='4', width=7, command=lambda: button_add(4))
button5 = Button(root, text='5', width=7, command=lambda: button_add(5))
button6 = Button(root, text='6', width=7, command=lambda: button_add(6))
button7 = Button(root, text='7', width=7, command=lambda: button_add(7))
button8 = Button(root, text='8', width=7, command=lambda: button_add(8))
button9 = Button(root, text='9', width=7, command=lambda: button_add(9))
button1.grid(row=1, column=1, padx=10, pady=10)
button2.grid(row=1, column=2, padx=10, pady=10)
button3.grid(row=1, column=3, padx=10, pady=10)
button4.grid(row=2, column=1, padx=10, pady=10)
button5.grid(row=2, column=2, padx=10, pady=10)
button6.grid(row=2, column=3, padx=10, pady=10)
button7.grid(row=3, column=1, padx=10, pady=10)
button8.grid(row=3, column=2, padx=10, pady=10)
button9.grid(row=3, column=3, padx=10, pady=10)
button_check = Button(root, text='Check PIN!', command=lambda:check_PIN())
button_check.grid(row=5, column=1)
root.mainloop()
Thank you for you answers in advance!
You arent setting number1_var
anywhere, so number1=number1_var.get()
is returning an empty string every time. But apart from that, what you'll want to do in check_PIN
is just call entry_PIN.get()
instead.
from tkinter import *
root = Tk()
root.geometry ('800x300')
root.title('Check PIN')
def button_add(number):
number1 = entry_PIN.get()
entry_PIN.delete (0, END)
entry_PIN.insert (0, number1+str(number))
pin = '1234'
def check_PIN():
if entry_PIN.get() == pin: # get the value from the Entry here
label1 = Label (root, text='Correct PIN!')
label1.grid(row=6, column=1)
else:
label2 = Label (root, text='Wrong PIN!')
label2.grid(row=7, column=1)
label_pin = Label (root, text='PIN:')
label_pin.grid(row=0, column=0, padx=10, pady=10, sticky='e')
entry_PIN = Entry(root)
entry_PIN.grid (row=0, column=1, padx=10, pady=10)
button1 = Button(root, text='1', width=7, command=lambda: button_add(1))
button2 = Button(root, text='2', width=7, command=lambda: button_add(2))
button3 = Button(root, text='3', width=7, command=lambda: button_add(3))
button4 = Button(root, text='4', width=7, command=lambda: button_add(4))
button5 = Button(root, text='5', width=7, command=lambda: button_add(5))
button6 = Button(root, text='6', width=7, command=lambda: button_add(6))
button7 = Button(root, text='7', width=7, command=lambda: button_add(7))
button8 = Button(root, text='8', width=7, command=lambda: button_add(8))
button9 = Button(root, text='9', width=7, command=lambda: button_add(9))
button1.grid(row=1, column=1, padx=10, pady=10)
button2.grid(row=1, column=2, padx=10, pady=10)
button3.grid(row=1, column=3, padx=10, pady=10)
button4.grid(row=2, column=1, padx=10, pady=10)
button5.grid(row=2, column=2, padx=10, pady=10)
button6.grid(row=2, column=3, padx=10, pady=10)
button7.grid(row=3, column=1, padx=10, pady=10)
button8.grid(row=3, column=2, padx=10, pady=10)
button9.grid(row=3, column=3, padx=10, pady=10)
button_check = Button(root, text='Check PIN!', command=lambda:check_PIN())
button_check.grid(row=5, column=1)
root.mainloop()