Klawiatura numeryczna PIN

0

Witam , wyświetla mi się taki koder ale pozwala wpisać tylko jedną liczbę. Potrzebuję żeby jak typowy czytnik przyjmował 4 liczby i żeby się one zakrywały **** jako domyślny pin ustawiłem 1234. Druga sprawa to żeby można było ustawić PIN i wpisać go do biblioteki GUI zacząłem to robić po przez json i bibliotekę os ale coś nie pykło. Z góry dziękuję za pomoc.

Zrzut ekranu 2023-10-25 180410.png

from tkinter import *
import json             
import os               
window = Tk()

def digitEntered(PIN):
    ''' Digit 0-9 button pressed event '''
    current_pin = PinLabel.cget('text')
    if current_pin == "PIN":
        PinLabel.configure(text=PIN)

def pinClear():
    ''' CLR button pressed event '''
    PinLabel.configure(text='PIN')

def pinEnter():
    ''' ENT button pressed event '''
    entered_pin = PinLabel.cget("text")  # Pobierz wprowadzony PIN
    if entered_pin == storedPin:  # Porównaj wprowadzony PIN z zapisanym PIN
        PinLabel.configure(text="PIN OK")  # Jeśli PIN jest zgodny, wyświetl "PIN OK"
    else:
        PinLabel.configure(text="Błąd PIN") # W przeciwnym razie wyświetl "Błąd PIN"

def pinBack():
    '''  button pressed event '''
    current_pin = PinLabel.cget("text")
    if current_pin != "PIN":
        new_pin = current_pin[:-1]  # Usuń ostatni znak z wprowadzonego PIN
        PinLabel.configure(text=new_pin)

# check to see if the data file "pin.txt" already exists
fname = "pin.txt"
if os.path.isfile(fname):
    # file exists so get data
    with open(fname) as json_file:
        storedPin = json.load(json_file)
else:
    # no such file so set defaults
    # stores the default PIN code to a file if no file found on the disk
    storedPin = '1234'
    with open(fname, 'w') as outfile:
        json.dump(storedPin, outfile)    

PinLabel=Label(window, relief = 'groove', width = 9, text='PIN')

# define the number pad buttons for the GUI
Digit0Btn = Button(window)
Digit1Btn = Button(window)
Digit2Btn = Button(window)
Digit3Btn = Button(window)
Digit4Btn = Button(window)
Digit5Btn = Button(window)
Digit6Btn = Button(window)
Digit7Btn = Button(window)
Digit8Btn = Button(window)
Digit9Btn = Button(window)

# a list of all the digits
Digits = [Digit0Btn,Digit1Btn,Digit2Btn,Digit3Btn,Digit4Btn,Digit5Btn,
          Digit6Btn,Digit7Btn,Digit8Btn,Digit9Btn]

EnterBtn = Button(window)
BackBtn = Button(window)
ClearBtn = Button(window)
ClearBtn.configure(text='CLR', height=3, width=5, command=pinClear)
EnterBtn.configure(text='ENT', height=3, width=5, command=pinEnter)
BackBtn.configure(text='<', command=pinBack)

PinLabel.grid(row=1, column=2)
BackBtn.grid(row=1,column=3)
Digit7Btn.grid(row=2, column=1)
Digit8Btn.grid(row=2, column=2)
Digit9Btn.grid(row=2, column=3)
Digit4Btn.grid(row=3, column=1)
Digit5Btn.grid(row=3, column=2)
Digit6Btn.grid(row=3, column=3)
Digit1Btn.grid(row=4, column=1)
Digit2Btn.grid(row=4, column=2)
Digit3Btn.grid(row=4, column=3)
Digit0Btn.grid(row=5, column=2)
ClearBtn.grid(row=5,column=1) 
EnterBtn.grid(row=5,column=3) 


# Label all the objects on the window and set their commands
ClearBtn.configure(text='CLR', height=3, width=5, command = pinClear)
EnterBtn.configure(text='ENT', height=3, width=5, command = pinEnter)
BackBtn.configure(text='<', command = pinBack)
''' NOTE: The lambda command means the value of the button is passed 
at run-time, and not when the loop is run!'''
for x in range(0,10):
    Digits[x].configure(text=x, height=3, width=5, 
                        command = lambda x = x : digitEntered(x))
    
    
#invoke the window
window.mainloop()
1
from tkinter import *  
import tkinter as tk
from functools import partial
from itertools import starmap
from time import time

root = Tk()
root.title("Terminal")
root.geometry("210x250")
root.resizable(False, False)

my_pin = []
stars = ""

def clear_list():
    global stars
    my_pin.clear()
    stars = ""
    print(my_pin) # do testow
    return label.config(text="PIN")
    
def list_append(num): 
    global stars   
    if len(my_pin) < 4:
        my_pin.append(num)
        print(my_pin) # do testow 
        stars += '*'
    return label.config(text=stars)
    #return label.config(text=my_pin) # do testow
    
    # -------do samodzielnego opracowania-------
def validation_pin():
	print(sum(my_pin)) # do testow 
	msg = ''
	if len(my_pin) < 4:
		msg += "incorrect"
	elif sum(my_pin) & 1 == 0:
		msg += "even number"
		print(f'Liczba parzysta')
	else:
		msg += "odd number"
		print(f'Liczba nieparzysta')
	root.after(3000, clear_list)
	return label.config(text=msg)

label = Label(root,relief=FLAT,text="PIN",width=20,heigh=4)
label.place(x=20,y=20)
label.pack()

x, y, count = 20, 40, 1
for i in range(0,9):
	if i % 3 != 0:
		x += 40
	else:
		y += 40
		x = 20
	btn = tk.Button(root, text = str(count), width = 3, command = partial(list_append,count))
	btn.pack()
	btn.place(x=x,y=y)
	x, count = (alfa + delta for alfa, delta in zip((x, count), (20, 1)))
	
btn_clear = Button(root, text = "CE", width = 3, command = clear_list) 
btn_clear.pack()
btn_clear.place(x=20,y=200)

btn_zero = Button(root, text = "0", width = 3, command = partial(list_append,0)) 
btn_zero.pack()
btn_zero.place(x=80,y=200)

btn_check = Button(root, text = "Acc", width = 3, command = validation_pin) 
btn_check.pack()
btn_check.place(x=140,y=200)

root.mainloop()

resztę funkcjonalności opracujesz sobie samodzielnie, proste operacje na liście i plikach

1 użytkowników online, w tym zalogowanych: 0, gości: 1