-2

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 GPIO

GPIO.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()
furas
  • 134,197
  • 12
  • 106
  • 148
RedstoneMaina
  • 27
  • 2
  • 8
  • show your code - what did you use - Tkinter, PyQt, wxPython or something other ? – furas Jan 07 '17 at 09:46
  • I think its Tkinter – RedstoneMaina Jan 07 '17 at 10:30
  • you can assign to every button function with argument - ie. `command=lambda:ledON("1")` and use `def ledON(arg):` to get this argument and remember on list. This way you can get your PIN. – furas Jan 07 '17 at 10:43
  • BTW: you can use `grid(row=0, column=0)` instead of `pack()` – furas Jan 07 '17 at 11:23
  • Thanks for the Grid suggestion, Ive created a code that makes a nice gui and it prints what you have pressed but I'm still a bit stumped on how to collect the data and create an output if the 4 number input matches the correct code or clear it if its incorrect, – RedstoneMaina Jan 12 '17 at 10:29
  • Heres my code so far... – RedstoneMaina Jan 12 '17 at 10:29
  • `code` from Tkinter import * import tkFont root = Tk() size = tkFont.Font(size = 40) def Code1(): print("1") def Code2(): print("2") def Code3(): print("3") def Code4(): print("4") def Code5(): print("5") def Code6(): print("6") def Code7(): print("7") def Code8(): print("8") def Code9(): print("9") def Code0(): print("0") def CodeC(): print("Cancel") def CodeSubmit(): print("Submit") – RedstoneMaina Jan 12 '17 at 10:34
  • root.title("KeyPad") button = Button(root, text='1', bg='lightblue', font = size, command = Code1) button.grid(row=0, column=0) button = Button(root, text='2', bg='lightblue', font = size, command = Code2) button.grid(row=0, column=1) button = Button(root, text='3', bg='lightblue', font = size, command = Code3) button.grid(row=0, column=2) button = Button(root, text='4', bg='lightblue', font = size, command = Code4) button.grid(row=1, column=0) button = Button(root, text='5', bg='lightblue', font = size, command = Code5) button.grid(row=1, column=1) – RedstoneMaina Jan 12 '17 at 10:35
  • button = Button(root, text='6', bg='lightblue', font = size, command = Code6) button.grid(row=1, column=2) button = Button(root, text='7', bg='lightblue', font = size, command = Code7) button.grid(row=2, column=0) button = Button(root, text='8', bg='lightblue', font = size, command = Code8) button.grid(row=2, column=1) button = Button(root, text='9', bg='lightblue', font = size, command = Code9) button.grid(row=2, column=2) button = Button(root, text='0', bg='lightblue', font = size, command = Code0) button.grid(row=3, column=1) – RedstoneMaina Jan 12 '17 at 10:36
  • button = Button(root, text='C', bg='red', font = size, command = CodeC) button.grid(row=3, column=0) button = Button(root, text='>', bg='green', font = size, command = CodeSubmit) button.grid(row=3, column=2) root.mainloop() – RedstoneMaina Jan 12 '17 at 10:36
  • sorry about spreading it across several posts im not yet sure how to put it all in one :P – RedstoneMaina Jan 12 '17 at 10:36
  • comment in not good place for code - you can edit question and add your code. – furas Jan 12 '17 at 11:45
  • you can define one function `def Code(value): print(value)` and assign to buttons like `command=lambda:Code("2")`, `command=lambda:Code("3")` – furas Jan 12 '17 at 11:47
  • create global/external list `pin = []` and inside `Code(value)` do `pin.append(value)` and you will have all numbers on the list – furas Jan 12 '17 at 11:50

1 Answers1

3

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 variable
    global 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 pin

        if 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 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)

furas
  • 134,197
  • 12
  • 106
  • 148
  • that link doesn't work anymore ? do you have more keypad examples? – master_yoda May 11 '20 at 07:20
  • 1
    @sunny_old_days remove `button-keypad` from link and you get all my tkinter's examples on GitHub. If you remove also `tkinter` then you get all my Python's examples on GitHub. – furas May 11 '20 at 07:57
  • 1
    @sunny_old_days I add new link to this code on GitHub because I moved it to subfolder. – furas May 11 '20 at 08:00