3

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) 

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks and how would i adjust my select(value) function
@user3907211: widget=keyboardFrame.focus_get(); widget.insert(...).
thanks for the clarification. i meant particularly within the if statement of select function
0

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.

5 Comments

thank you. I am just getting one small error: Unresolved reference 'FocusIn'
This is a bit of overkill, since tkinter already remembers which widget has focus. There's really no need to track it yourself.
@user3907211: sorry, forgot the quotes.
thanks for that, i've added the updated code under the update section of my initial post. It doesn't seem to respond and receive errors such as in select inputFocus.insert(END, value) AttributeError: 'NoneType' object has no attribute 'insert'
@user3907211: now it works. I also forgot that the function in bind takes an event as argument.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.