代码之家  ›  专栏  ›  技术社区  ›  Zelimir_Horvat_CRO

PIN面板显示“PIN错误”信息,无论PIN是否正确

  •  0
  • Zelimir_Horvat_CRO  · 技术社区  · 11 月前

    我有一个任务是在GUI中制作PIN面板,该面板应该检查PIN是否正确,并给出“正确PIN”或“错误PIN”的消息。我设法编写了整个代码,但即使我写了正确的PIN,它也会返回“错误的PIN”消息。

    我试图更改字符串和整数中的PIN类型,更改调用函数check_PIN()的位置,但我总是得到相同的(错误的)输入

    代码:

    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()
    

    感谢您提前回复!

    1 回复  |  直到 11 月前
        1
  •  1
  •   JRiggles    11 月前

    您没有设置 number1_var 任何地方,所以 number1=number1_var.get() 每次都返回一个空字符串。但除此之外,你想在 check_PIN 只是打个电话 entry_PIN.get() 相反。

    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()