Tkinter Python Entry状态属性

时间:2011-07-25 03:42:08

标签: python tkinter

我试图通过检查Entries状态值来检查用户光标是否在文本框中。但是,我得到“AttributeError:条目实例没有属性'状态'”。如何检查用户光标是否在文本框中?

import os

from Tkinter import *

textboxes = []

def read_textboxes():
    for e in textboxes:
        print e.state

root = Tk()
for i in range(5):
    textboxes.append(Entry(root))
    textboxes[i].insert(0,"I am textbox #"+str(i))
    textboxes[i].grid(row = i,column = 0)
button = Button(root,text = "Read Textboxes",command = read_textboxes)
button.grid(row = len(textboxes),columnspan = 99,pady = 10)
mainloop()

1 个答案:

答案 0 :(得分:1)

您可以在根目录上调用focus_get()以获取当前具有焦点的窗口小部件。

def read_textboxes():
    in_focus = root.focus_get()
    for e in textboxes:
        print e == in_focus