我正在使用Python v3.4并使用Tkinter
库来训练我的GUI。
我在我的python应用程序中包含了一个屏幕键盘。问题如下:
他们是三个条目:
username = Entry(root,font="Verdana 22")
password = Entry(root, font="Verdana 22")
courrierEntry = Entry(root, font="Verdana 22")
下面是键盘
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
正如您在键盘上注意到的那样,它仅指向username entry
,因此即使按下按钮时,只有username entry
只有username entry
时才会对#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)
没有注意力。
我希望建立某种if语句,问题是我不熟悉验证条目是否被集中/选中。它应该通过当前选择的条目而不是名称。因此,基本上每当我选择时我都可以输入。
更新
class Organization < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
答案 0 :(得分:3)
您可以调用keyboardFrame.focus_get()
来获取具有焦点的窗口对象。
答案 1 :(得分:0)
你想要的是记住最后一个有焦点的条目。 因为按下按钮会将焦点对准按钮。
hasFocus = None
def currentEntry(myEntry):
global hasFocus
hasFocus = myEntry
username.bind("<FocusIn>", lambda myEvent: currentEntry(username)) # do the same for the other entries
然后在select中使用hasFocus。
注意:您可能希望添加
if inputFocus != None:
处于选择状态,因为在开头,没有选定的条目。