-2

我正在尝试用python中的guizero制作一个简单的小键盘,其中每个数字都有一个按钮,当按下按钮时,它们会将它们的数字返回到一个文本框中,在文本框中使用屏幕小键盘输入PIN或通过键盘检查文本框的值是否与存储在名为 pin 的变量中的值相同。小键盘上的 enter 按钮与按键盘上的 enter 相同。我尝试了很多东西,但是当从按钮调用的函数返回时,会抛出错误,或者该函数会在没有按下按钮的情况下设置文本框值。如果有人可以向我发送一些为我执行此操作的编辑,我将非常感激。小键盘布局:1 2 3 4 5 6 7 8 9 0

进入

这是我到目前为止的代码(我只有 3 个按钮来节省视觉空间,而且我知道所有 10 个按钮都必须复制它们:

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

谢谢你们

4

1 回答 1

1

我也在制作一个数字键盘来与我的树莓派一起使用。这是我的代码。您可以调整它以使用您想要的任何方法。但基本上你将输入分配为一种数组,当有人点击按钮时,它会将该数字添加到作为输入的“数组”中。我确信有更好更快的方法来做到这一点,但老实说我不知道​​ python,只知道 c++,所以我不知道。我还没有用我的 pi 对此进行测试,但我认为它会起作用。

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
于 2019-01-14T19:03:08.037 回答