我是一个新手,我有一个小脚本,它使用guizero创建一个小应用程序来读取rfid标签并激活门锁。我的问题是,当电源关闭,覆盆子皮重新启动。脚本启动,但我必须在文本框中手动单击它才能开始工作。如何让它在启动时自动将焦点设置在文本框上?谢谢马克
下面是代码,我在网上找到了这个项目,并修改了一些来为我工作。
from gpiozero import LED, Buzzer
from guizero import App, Box, Text, TextBox, warn
import gpiozero
import csv
RELAY_PIN = 17
led8 = LED(5)
led9 = LED(6)
relay= gpiozero.OutputDevice(RELAY_PIN,active_high=False, initial_value=False)
def clearDisplay():
print("Clear display")
rfidStatus.value = "—"
rfidText.value = ""
led8.off()
led9.off()
relay.off()
rfidStatus.repeat(1000, checkRFidTag)
def checkRFidTag():
tagId = rfidText.value
if tagId != "":
RFidRegistered = False
print(tagId)
with open("Database.csv") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row["RFid"] == tagId:
RFidRegistered = True
print("Welcome " + row["User"])
rfidStatus.value = "Welcome " + row["User"]
led8.on()
relay.toggle()
rfidStatus.after(5000, clearDisplay)
if RFidRegistered == False:
print("RFid tag is not registered")
rfidStatus.value = "RFid tag is not registered"
led9.on()
rfidStatus.after(3000, clearDisplay)
rfidStatus.cancel(checkRFidTag)
app = App(title="RFID EM4100 Simple GUI", width=350, height=150, layout="auto")
instructionText = Text(app, text="Click on the text button below\nand scan your RFid tag.")
rfidText = TextBox(app,text="")
rfidStatus = Text(app, text="—")
rfidStatus.repeat(1000, checkRFidTag)
#designBy = Text(app, text="Design by Idris – Cytron Technologies", align="bottom")
app.display()
view发布于 2022-10-16 15:36:14
启动后不久,您可以使用与应用程序关联的计时器来调用功能。例如,使用:
app.after(1000,setFocus)打电话:
def setFocus():
nameOfTextBox.focus()下面的示例设置两个文本框和两个按钮。通过注释掉setFocus中的一行,它在一秒后将焦点设置为右侧文本框或“取消”按钮:
from guizero import App, Text, Box, TextBox, PushButton
import sys
app = App(title="Give Focus")
app.width = 500
app.height = 100
paddingHeight = 1
paddingWidth = 10
buttonWidth = 10
topBoxHeight = 100
bottomBoxHeight = 100
app.height = topBoxHeight + bottomBoxHeight
def setFocus():
rightTextBox.focus()
# buttonCancel.focus()
def call_exit():
buttonCancel.text="Exiting"
buttonCancel.enabled=False
app.update()
app.destroy()
sys.exit()
topBox = Box(app, align="top", width= "fill", height = topBoxHeight, border= True)
textFieldBox = Box(topBox, align="top", width= "fill", height= "fill", border= True, layout = "grid")
paddingT00 = Text(textFieldBox, text="", width = paddingWidth, height = paddingHeight, grid = [0,0])
paddingT10 = Text(textFieldBox, text="", width = paddingWidth, height = paddingHeight, grid = [1,0])
message = Text(textFieldBox, text="Text Field Box", grid = [2,0])
paddingT01 = Text(textFieldBox, text="", width = paddingWidth, height = paddingHeight, grid = [0,1])
leftTextBox = TextBox(textFieldBox, text="Type here", width = paddingWidth, height = 1, grid = [1,1])
paddingT21 = Text(textFieldBox, text="", width = paddingWidth, height = paddingHeight, grid = [2,1])
rightTextBox = TextBox(textFieldBox, text="...or here", width = paddingWidth, height = 1, grid = [3,1])
bottomBox = Box(app, align="top", width= "fill", height = bottomBoxHeight, border= True)
buttonBox = Box(bottomBox, align="top", width= "fill", height= "fill", border= True, layout = "grid")
paddingB00 = Text(buttonBox, text="", width = paddingWidth, height = paddingHeight, grid = [0,0])
paddingB10 = Text(buttonBox, text="", width = paddingWidth, height = paddingHeight, grid = [1,0])
message = Text(buttonBox, text="Button Box", grid = [2,0])
paddingB01 = Text(buttonBox, text="", width = paddingWidth, height = paddingHeight, grid = [0,1])
buttonOK = PushButton(buttonBox, text="OK", width = paddingWidth, height = 1, grid = [1,1])
paddingB21 = Text(buttonBox, text="", width = paddingWidth, height = paddingHeight, grid = [2,1])
buttonCancel = PushButton(buttonBox, text="Cancel", command = call_exit, width = paddingWidth, height = 1, grid = [3,1])
app.after(1000,setFocus)
app.display()https://stackoverflow.com/questions/72706855
复制相似问题