需要一个用于在代码正确时输出引脚的触摸屏的 Gui 键盘
Posted
技术标签:
【中文标题】需要一个用于在代码正确时输出引脚的触摸屏的 Gui 键盘【英文标题】:Need a Gui Keypad for a touchscreen that outputs a pin when code is correct 【发布时间】:2017-01-07 08:27:31 【问题描述】:我有一个带有运行 raspbian 的触摸屏的树莓派,我希望在触摸屏上有一个带有数字键盘的 Gui,当输入正确的输入时,一个 pin 将输出到门闩或其他东西。我已经完成了一个带有数字的 Gui(通过 Python),但我无法让几个数字并排放置。任何信息都会对此有所帮助谢谢:) 这是我用来尝试放置按钮的代码(你可以看到我只是使用了一个简单的 LED 开/关按钮 Gui 并用它来查看按钮的位置)
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()
【问题讨论】:
显示您的代码 - 您使用了什么 - Tkinter、PyQt、wxPython 或其他? 我认为是 Tkinter 您可以使用参数分配给每个按钮功能 - 即。command=lambda:ledON("1")
并使用 def ledON(arg):
获取此参数并记住列表。这样您就可以获得您的 PIN 码。
顺便说一句:你可以用grid(row=0, column=0)
代替pack()
感谢 Grid 的建议,我创建了一个代码,可以制作一个很好的 gui,它会打印你按下的内容,但我仍然对如何收集数据和创建输出感到有点困惑,如果4 数字输入匹配正确的代码,如果不正确则清除它,
【参考方案1】:
带键盘的简单示例:
我使用全局字符串变量pin
来保留所有按下的数字。
(你可以用list
代替string
)
键*
删除最后一个数字,键#
将pin
与文本"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()
GitHub:furas/python-examples/tkinter/__button__/button-keypad
(编辑:我更改了指向 GitHub 的链接,因为我将代码移到了子文件夹)
【讨论】:
那个链接失效了?你有更多的小键盘示例吗? @sunny_old_days 从链接中删除button-keypad
,您可以在 GitHub 上获得我的 tkinter 的所有示例。如果您还删除了tkinter
,那么您将在 GitHub 上获得我所有的 Python 示例。
@sunny_old_days 我在 GitHub 上添加了此代码的新链接,因为我已将其移至子文件夹。以上是关于需要一个用于在代码正确时输出引脚的触摸屏的 Gui 键盘的主要内容,如果未能解决你的问题,请参考以下文章