code_legend
code_legend

Reputation: 3285

check which entry is focused (Python)

I am working with Python v3.4 and using Tkinter library to workout my GUI.

I have included an on-screen keyboard in my python app. The problem is as follow:

They are three entries:

username = Entry(root,font="Verdana 22")

password = Entry(root, font="Verdana 22")

courrierEntry = Entry(root, font="Verdana 22")

Below is the keyboard

 def select(value):
        if value == "BACK":
            entry2 = len(username.get()[:-1])

        username.delete(entry2, END)

    elif value == " Space ":
            username.insert(END, ' ')

    elif value == " Tab ":
            username.insert(END, '    ')

    else:
            username.insert(END, value)

buttons = [
    'q','w','e','r','t','y','u','l','o','p','BACK','7','8','9','-',
    'a','s','d','f','g','h','j','k','l','[',']','4','5','6','+',
    'z', 'x','c','v','b','n','m',',','.','?','1','2','3','/','HELP',
    ]

varRow = 2
varColumn = 0

for button in buttons:

    command = lambda x = button: select(x)
    keyboardButton = Button(keyboardFrame, text = button, width = 8,  bg="#795548", fg="#ffffff",
            activebackground="#D7CCC8",activeforeground="#000000",relief = "raised", padx=0,font="Verdana 11 bold",
            pady=8, bd=8, command = command)
    keyboardButton.grid(row = varRow, column = varColumn)
    varColumn += 1
    if varColumn > 14 and varRow == 2:
        varColumn = 0
        varRow += 1
    if varColumn > 14 and varRow == 3:
        varColumn = 0
        varRow +=1

As you may have notice in the keyboard, it only refers to the username entry, and hence even when not focused on the username entry whenever a button is pressed only the username entry reacts.

I would want that to establish some kind of if statement, the problem is that I am not familiar with verifying if the entry is focused/selected. It should go by the entry currently selected and not by name. So essentially whenever I select I can type.

Update:

------KEYBOARD----------

#checking to see whcih input entry is currently focused
inputFocus = None

def currentEntry(userEntry):
    global inputFocus
    inputFocus = userEntry

username.bind("<FocusIn>", lambda: currentEntry(username))
password.bind("<FocusIn>", lambda: currentEntry(password))
courrierEntry.bind("<FocusIn>", lambda: currentEntry(courrierEntry))


def select(value):
    if value == "BACK":
        entry2 = len(inputFocus.get()[:-1])

        inputFocus.delete(entry2, END)

    elif value == " Space ":
            inputFocus.insert(END, ' ')

    elif value == " Tab ":
            inputFocus.insert(END, '    ')

    else:
            inputFocus.insert(END, value)

Upvotes: 3

Views: 1556

Answers (2)

Eric Levieil
Eric Levieil

Reputation: 3574

What you want is to remember the last Entry that had focus. Because pressing a button will give the focus to the button.

hasFocus = None

def currentEntry(myEntry):
    global hasFocus 
    hasFocus = myEntry

username.bind("<FocusIn>", lambda myEvent: currentEntry(username)) # do the same for the other entries

Then use hasFocus in select.

NB: you probably wish to add if inputFocus != None: in select because at the beginning, there is no selected Entry.

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 386342

You can call keyboardFrame.focus_get() to get the window object that has the focus.

Upvotes: 3

Related Questions