Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1579

Python • How to clear stored variable data while app / window is in mainloop

$
0
0
I took someone else's code that I found online for a keypad / pin checking system and expanded it so that it can check several pins instead of just one. I presume it's ok post my version since it's now very different to theirs. In any case, it is only to get some clarity on an issue I have with it.

Pin numbers are imported from a file called PINs.py that stores them in a variable (PIN1, PIN2, etc) and are checked within the

Code:

pin_check
function with a set of "if" statement actions for each outcome.

The problem is even though one of the pin numbers has been used successfully (say, PIN1) and subsequently changed (a python script changes the pin right after use), entering the new pin that is now stored in the PINs.py file doesn't work unless I restart the program. I guess restarting the program clears that old stored pin number.

As a test, I included a print statment

Code:

print("current stored PIN1:", PIN1)
to see what is there and it displays the previously entered and accepted pin number. In VS code however, when I hover my mouse over the variable for PIN1, I see the correct pin which is stored in the PINS.py file. How can I get this code to "forget" the previously used pin number so that I can enter the new and current pin?


The code below has most of the "if" statement actions and formatting removed for ease of reading.

Code:

import tkinter as tkfrom PINS import *def pin_check(value):    global pin    if value == '*':        pin = pin[:-1]        e.delete('0', 'end')        e.insert('end', pin)    elif value == '#':        if pin == PIN1:            print("PIN1 OK")            pin = ''            e.delete('0', 'end')        elif pin == PIN2:            print("PIN2 OK")            pin = ''            e.delete('0', 'end')                  elif pin == PIN3:            print("PIN3 OK")            pin = ''            e.delete('0', 'end')                 else:            print("WRONG PIN")            pin = ''            e.delete('0', 'end')    else:        pin += value        e.insert('end', value)keys = [    ['1', '2', '3'],        ['4', '5', '6'],        ['7', '8', '9'],        ['*', '9', '#'],  ]pin = '' root = tk.Tk()e = tk.Entry(root, show="*")e.grid(row=0, column=0, columnspan=3, ipady=5)for y, row in enumerate(keys, 1):    for x, key in enumerate(row):        b = tk.Button(root, text=key, command=lambda val=key:code(val))        b.grid(row=y, column=x, ipadx=10, ipady=10)root.mainloop()
EDIT: added same pin clearing syntax for PIN1 check as in the full code it is also there

Statistics: Posted by Furutsu — Tue Dec 09, 2025 12:04 pm



Viewing all articles
Browse latest Browse all 1579

Trending Articles