shallnot
shallnot

Reputation: 1

Guizero textbox focus

I am a newb and I have a small script that uses guizero to create a small app that reads a rfid tag and activates door lock. The problem I am having is, when the power goes off and the raspberry pi reboots. the script launches but I have to manually click in the texts box before it will start working. how can I have it automatically set focus on the textbox when it launches? thanks Mark

Here is code, I found this project online and modified some to work for me.

    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

Upvotes: 0

Views: 274

Answers (1)

AndyC
AndyC

Reputation: 21

You could use a timer associated with the app to call a fuction shortly after starting up. For example, use:

        app.after(1000,setFocus)

to call:

        def setFocus():
            nameOfTextBox.focus()

The example below sets up two text boxes and two buttons. By commenting out one or other of the lines in setFocus it sets focus to the right hand text box or the cancel button after one second:

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

Upvotes: 0

Related Questions