Need a Gui Keypad for a touchscreen that outputs a pin when code is correct

2024/11/18 19:53:53

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 output to a door latch or something. I have been over to make a Gui with a number on (by Python) it but i cant get several numbers to sit next to each other. any info will help on this thanks :) This is the code I used to try and place the buttons (you can see i just used a simple LED on/off button Gui and used it to see the placement of the buttons)

from Tkinter import *
import tkFont
import RPi.GPIO as GPIOGPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.OUT)
GPIO.output(40, GPIO.LOW)win = Tk()myFont = tkFont.Font(family = 'Helvetica', size = 36, weight = 'bold')def ledON():print("LED button pressed")if GPIO.input(40) :GPIO.output(40,GPIO.LOW)ledButton["text"] = "LED OFF"else:GPIO.output(40,GPIO.HIGH)ledButton["text"] = "LED ON"def exitProgram():print("Exit Button pressed")GPIO.cleanup()win.quit()  win.title("LED GUI")exitButton  = Button(win, text = "1", font = myFont, command = ledON, height     =2 , width = 8) 
exitButton.pack(side = LEFT, anchor=NW, expand=YES)ledButton = Button(win, text = "2", font = myFont, command = ledON, height = 2, width =8 )
ledButton.pack(side = TOP, anchor=CENTER, expand=YES)ledButton = Button(win, text = "3", font = myFont, command = ledON, height = 2, width =8 )
ledButton.pack(side = RIGHT, anchor=NE, expand=YES)ledButton = Button(win, text = "4", font = myFont, command = ledON, height = 2, width =8 )
ledButton.pack(side = TOP, anchor=W, expand=YES)ledButton = Button(win, text = "5", font = myFont, command = ledON, height = 2, width =8 )
ledButton.pack(side = TOP, anchor=W, expand=YES)ledButton = Button(win, text = "6", font = myFont, command = ledON, height = 2, width =8 )
ledButton.pack(side = TOP, anchor=W, expand=YES)ledButton = Button(win, text = "7", font = myFont, command = ledON, height = 2, width =8 )
ledButton.pack(side = TOP, anchor=W, expand=YES)ledButton = Button(win, text = "8", font = myFont, command = ledON, height = 2, width =8 )
ledButton.pack(side = TOP, anchor=W, expand=YES)ledButton = Button(win, text = "9", font = myFont, command = ledON, height = 2, width =8 )
ledButton.pack(side = TOP, anchor=N, expand=YES)ledButton = Button(win, text = "0", font = myFont, command = ledON, height = 2, width =8 )
ledButton.pack(side = TOP, anchor=NW, expand=YES)mainloop()
Answer

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 ---def code(value):# inform function to use external/global variableglobal pinif 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 pinpin += 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 stringroot = 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 in enumerate(keys, 1):for x, key in enumerate(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)

https://en.xdnf.cn/q/120037.html

Related Q&A

How can i make the python to wait till i complete speaking?

I am writing a program to recognise the speech from a microphone and the code will process accordingly. The code I wrote for this purpose is below.import speech_recognition as sr import webbrowser impo…

Facing AttributeError: list object has no attribute lower

I have posted my sample train data as well as test data along with my code. Im trying to use Naive Bayes algorithm to train the model.But, in the reviews Im getting list of list. So, I think my code is…

why I cannot use max() function in this case? [duplicate]

This question already has answers here:Why do I get "TypeError: int object is not iterable" when trying to sum digits of a number? [duplicate](4 answers)Closed 1 year ago.n,m,k=map(int, inpu…

SQLALchemy and Python - Getting the SQL result

I am using cloudkitty which is rating module in OpenStacks.But here question is regarding the SQLAlchemy and Python.I am new to SQLAlchemy.I need to fetch some details from a table using a API call.So …

ValueError: invalid literal for int() with base 10: python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Closed 10 years ago.Questions asking for code must demonstrate a minimal understanding of the proble…

python code to connect to sftp server

I found this code to connect to remote sftp server with the help of username ,password and host but i also need to include the port number, can any one let em know how to include the port number in thi…

Python: get the max value with the location above and below than the max

If I have a dataframe like this, index User Value location1 1 1.0 4.5 2 1 1.5 5.23 1 3.0 7.04 1 2.5 7.55 2 1.0 11.56 2 1.…

Retrieve smart cards PAN with Python and pyscard

Im trying to retrieve the PAN of a smart card using pyscard in Python. What I have done so far is to connect to the reader and to retrieve various information about the reader and the card... but I can…

How to stop a specific key from working in Python

My laptop keyboard has a bug and it sometimes presses the number 5 randomly so i tried many things and they didnt work, I tried programming a code that can stop it but i couldnt because i am a beginner…

How do i sort a 2D array or multiple arrays by the length of the array using bubble sort

trying to write a Python function: def compare_lengths(x, y, z) which takes as arguments three arrays and checks their lengths and returns them as a triple in order of length. For example, if the funct…