Нужна клавиатура Gui для сенсорного экрана, который выводит пин-код, когда код правильный
У меня Raspberry Pi с сенсорным экраном работает Rasbian, я надеюсь, что на сенсорном экране есть Gui с цифровой клавиатурой, которая при вводе правильного ввода выводит вывод на дверную защелку или что-то в этом роде. Я закончил, чтобы сделать Gui с номером на (от Python), но я не могу получить несколько номеров, чтобы сидеть рядом друг с другом. любая информация поможет в этом спасибо:) Это код, который я использовал, чтобы попытаться разместить кнопки (вы можете видеть, что я просто использовал простой светодиодный индикатор включения / выключения кнопки 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()
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: https://github.com/furas/python-examples/tree/master/tkinter/button-keypad