Skip to content Skip to sidebar Skip to footer

Need A Gui Keypad For A Touchscreen That Outputs A Pin When Code Is Correct

I have a raspberry pi with a touchscreen running raspbian, I'm hoping to have a Gui on the touchscreen that had a number keypad that when a correct input is entered a pin will outp

Solution 1:

Simple example with keypad:

I use global string variable pin to keep all pressed numbers. (You can use list instead of string)

Key * removes last number, key # compares pin with text "3529"

import tkinter as tk

# --- functions ---defcode(value):

    # inform function to use external/global variableglobal pin

    if value == '*':
        # remove last number from `pin`
        pin = pin[:-1]
        # remove all from `entry` and put new `pin`
        e.delete('0', 'end')
        e.insert('end', pin)

    elif value == '#':
        # check pinif pin == "3529":
            print("PIN OK")
        else:
            print("PIN ERROR!", pin)
            # clear `pin`
            pin = ''# clear `entry`
            e.delete('0', 'end')

    else:
        # add number to pin
        pin += value
        # add number to `entry`
        e.insert('end', value)

    print("Current:", pin)

# --- main ---

keys = [
    ['1', '2', '3'],    
    ['4', '5', '6'],    
    ['7', '8', '9'],    
    ['*', '9', '#'],    
]

# create global variable for pin
pin = ''# empty string

root = tk.Tk()

# place to display pin
e = tk.Entry(root)
e.grid(row=0, column=0, columnspan=3, ipady=5)

# create buttons using `keys`for y, row inenumerate(keys, 1):
    for x, key inenumerate(row):
        # `lambda` inside `for` has to use `val=key:code(val)` # instead of direct `code(key)`
        b = tk.Button(root, text=key, command=lambda val=key:code(val))
        b.grid(row=y, column=x, ipadx=10, ipady=10)

root.mainloop()

enter image description here

GitHub: furas/python-examples/tkinter/__button__/button-keypad

(EDIT: I changed link to GitHub because I moved code to subfolder)

Post a Comment for "Need A Gui Keypad For A Touchscreen That Outputs A Pin When Code Is Correct"