-2

I'm trying to make a simple numpad made with guizero in python, where each number has a button, and when the buttons are pressed, they return their number into a text box, where the text box had the PIN entered with the onscreen numpad or by keyboard and check whether the value of the text box is the same as that stored in a variable called pin. The enter button on the numpad will do the same as pressing enter on the keyboard. I have tried a lot of things, but when returning from the function that the button calls, an error throws, or the function would set the text box value without the button being pressed. If someone can send me some edits that does this for me I woukd be very grateful. Numpad layout: 1 2 3 4 5 6 7 8 9 0

Enter

This is the code i have so far (i have only 3 buttons to save visual space, and i know that for all 10 buttons ill have to just duplicate them:

import guizero as gz

global pin
pin = ""

def pressed_1(number):
    text_pin.set(number)
def pressed_2(number):
    text_pin.set(number)
def pressed_3(number):
    text_pin.set(number)

def main():
    app = gz.App(title="pin",layout="grid")
    global text_pin
    text_pin = gz.TextBox(app,text=pin,grid=[0,0])
    button_1 = gz.PushButton(app,text="1",command=pressed_1("1"),grid=[1,1])
    button_2 = gz.PushButton(app,text="2",command=pressed_2("2"),grid=[2,1])
    button_3 = gz.PushButton(app,text="3",command=pressed_3("3"),grid=[3,1])
    app.display()
main()

Thank you all

benbayliss24
  • 33
  • 1
  • 5

1 Answers1

1

I too am making a digital keypad to use with my raspberry pi. Here's my code. You could adapt it to use whatever method you want. But basically you're assigning the input as kind of an array and when someone clicks on the button, it adds that number to the "array" which is the input. I'm sure there is a better and faster way of doing this, but I honestly don't know python, only c++, so I wouldn't know. I haven't tested this out with my pi yet, but I think it'll work.

from guizero import App, Text, TextBox, PushButton
def fetch_response():
    user = submits.value
    if user == "1234":
        sleep(1)
        app.hide()

def Keypad__1():
   submits.append('1')
def Keypad__2():
   submits.append('2')
def Keypad__3():
   submits.append('3')
def Keypad__4():
   submits.append('4')
def Keypad__5():
   submits.append('5')
def Keypad__6():
   submits.append('6')
def Keypad__7():
   submits.append('7')
def Keypad__8():
   submits.append('8')
def Keypad__9():
   submits.append('9')
def Keypad__0():
   submits.append('0')
def Clearapp():
   submits.clear()
submit = PushButton(app, fetch_response, text="Submit", grid=[410, 300])
Clear_app = PushButton(app, Clearapp, text="Clear", grid=[410, 325])
app = App(title="KeyPad", width=800, height=480, layout="grid")
Keypad_1 = PushButton(app, Keypad__1, text="1", grid=[0, 400])
Keypad_1.width = 8
Keypad_1.height = 4
Keypad_2 = PushButton(app, Keypad__2, text="2", grid=[50, 400])
Keypad_2.width = 8
Keypad_2.height = 4
Keypad_3 = PushButton(app, Keypad__3, text="3", grid=[100, 400])
Keypad_3.width = 8
Keypad_3.height = 4
Keypad_4 = PushButton(app, Keypad__4, text="4", grid=[0, 450])
Keypad_4.width = 8
Keypad_4.height = 4
Keypad_5 = PushButton(app, Keypad__5, text="5", grid=[50, 450])
Keypad_5.width = 8
Keypad_5.height = 4
Keypad_6 = PushButton(app, Keypad__6, text="6", grid=[100, 450])
Keypad_6.width = 8
Keypad_6.height = 4
Keypad_7 = PushButton(app, Keypad__7, text="7", grid=[0, 500])
Keypad_7.width = 8
Keypad_7.height = 4
Keypad_8 = PushButton(app, Keypad__8, text="8", grid=[50, 500])
Keypad_8.width = 8
Keypad_8.height = 4
Keypad_9 = PushButton(app, Keypad__9, text="9", grid=[100, 500])
Keypad_9.width = 8
Keypad_9.height = 4
Keypad_0 = PushButton(app, Keypad__0, text="0", grid=[50, 550])
Keypad_0.width = 8
Keypad_0.height = 4
Sam
  • 11
  • 1