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

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

$
0
0
Place the pins in a file and persist new pins in this file.
Easy done if file format is json:

Code:

{"PIN1": "1111", "PIN2": "2222", "PIN3": "3333"}
For convenience, wrap all the pin functionality in one class.
Some sample

Code:

import jsonimport tkinter as tkclass PinStorage:    def __init__(self):        self._filename = "pins.json"        # default settings        self._pins = { "PIN1": "1111", "PIN2": "2222", "PIN3": "3333"}                pins = self._read_pins()        if pins is None:            self._write_pins(self._pins)        else:            self_pins = pins                def _read_pins(self):        try:            with open(self._filename, "r") as f:                data = f.read()                pins = json.loads(data)                return pins        except Exception as e:            print(e)        return None        def _write_pins(self, pins):        try:            with open(self._filename, "w") as f:                data = json.dumps(pins)                f.write(data)                print( f"data written to {self._filename}")        except Exception as e:            print(e)                def set_pin1(self, pin:str):        self._set("PIN1", pin)            def set_pin2(self, pin:str):        self._set("PIN2", pin)            def set_pin3(self, pin:str):        self._set("PIN3", pin)            def _set(self, key:str, pin:str):        self._pins[key] = pin        self._write_pins(self._pins)    def get_pin1(self) -> str:        return self._pins["PIN1"]            def get_pin2(self) -> str:        return self._pins["PIN2"]            def get_pin3(self) -> str:        return self._pins["PIN3"]                pin_storage = PinStorage()def pin_check(value):    global pin    if value == '*':        pin = pin[:-1]        e.delete('0', 'end')        e.insert('end', pin)    elif value == '#':        if pin == pin_storage.get_pin1():            print("PIN1 OK")            pin = ''            e.delete('0', 'end')        elif pin == pin_storage.get_pin2():            print("PIN2 OK")            pin = ''            e.delete('0', 'end')                  elif pin == pin_storage.get_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 = '' def code(val):    pin_check(val)    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)# some test codeb = tk.Button(root, text="set new PIN1 to 9999", command=lambda :pin_storage.set_pin1("9999"))b.grid(row=5, column=0, columnspan=3, ipady=5)root.mainloop()

Statistics: Posted by ghp — Tue Dec 09, 2025 3:26 pm



Viewing all articles
Browse latest Browse all 1579

Trending Articles