vendredi 24 juillet 2015

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.

Aucun commentaire:

Enregistrer un commentaire