Tk를 사용하여 파이썬 버전 3의 입력 및 텍스트 필드에 대한 포커스 이벤트를 이해하는 데 어려움을 겪고 있습니다. 라디오 옵션이나 버튼을 클릭하면 결국 엔 엔트리 박스의 유효성을 검사해야합니다.포커스 이벤트 (또는 그 부재)
아래 코드를 실행하면 다른 곳에 필요한 유효성 검사가 아닌 포커스 문제를 보여줍니다. 상단 행 입력 상자 중 하나에 커서를 놓고 다른 위젯들 사이를 클릭하십시오. 포커스 아웃 이벤트는 입력 (즉, 텍스트/입력 상자)을 허용하는 위젯에 발생합니다.
버튼 또는 라디오 옵션을 클릭하면 커서가 입력 또는 텍스트 상자 위젯에 남아 있습니다. 왜 내가 라디오 옵션이나 버튼에 집중했는지.
나는 FocusIn/Out 이벤트를 시도했지만 여전히 기쁨이 없습니다. 누구든지 설명이 있다면 나는 그것을 왜 극복 할 수 있는지, 그리고 어쩌면 내가 어떻게 극복 할 수 있는지에 대해 흥미를 느끼게 될 것입니다.
from tkinter import *
root = Tk()
root.title("My Widgets")
root.update_idletasks()
root.geometry("350x200+10+300")
root.attributes("-toolwindow",1)
root.resizable(width=FALSE, height=FALSE)
root.config(bg="blue")
# function below output to the console and label the focus results
def Validate(a,b,c,d,e,f,g,h):
text = g + ' on ' + h
lblOutputVar.set(text)
print(f,g,h)
return True
var = IntVar()
lblOutputVar = StringVar()
vcmd=(root.register(Validate),'%d','%i','%P','%s','%S','%v','%V','%W')
entryOne = Entry(root, name = 'entryBoxOne')
entryOne.config(validate = 'all',vcmd=vcmd)
entryOne.grid(row=1, column=1,padx=(0,0),pady=(10,10),ipady=(1), sticky=E+W)
entryTwo = Entry(root, name = 'entryBoxTwo')
entryTwo.config(validate = 'all',vcmd=vcmd)
entryTwo.grid(row=1, column=2,padx=(10,0),pady=(10,10),ipady=(1), sticky=E+W)
txtBox = Text(root, name = 'textBox', width=10, height=1, takefocus = 0)
txtBox.grid(row=5, column=1, sticky=E+W)
aButton = Button(root, text = 'Click Me!', takefocus=1)
aButton.grid(row=5, column=2)
lblOutput = Label(root, name = 'labelOutput', width=20, height=2, textvariable=lblOutputVar)
lblOutput.grid(row=10, column=1, columnspan =2, pady=(5,0), sticky=E+W)
radioOne = Radiobutton(root, anchor = 'w', text = 'One', variable = var, value = 1, takefocus = 1)
radioOne.grid(row=2, column=1, sticky=E+W)
radioTwo = Radiobutton(root, anchor = 'w', text = 'Two', variable = var, value = 2, takefocus = 1)``
radioTwo.grid(row=3, column=1, sticky=E+W)
root.mainloop()
확인해 주셔서 감사합니다. 나는 그것이 우승을해야만하는 특질 중 하나라고 생각합니다. 단추로 'takefocus'를 키보드 탐색에 사용할 수있는 옵션이 이상하게 생겨났습니다. –
@DrJeep : 마우스를 사용할 수없는 경우 단추와 상호 작용할 수 있어야하며, 단추를 키보드 포커스로 가져올 수있는 유일한 방법이 있습니다. 접근성 문제입니다. 내가 이상하다고 생각하는 것은 단추 모양의 위젯이 클릭 할 때 초점을 맞추는 것입니다. 그 점은 ttk에서 일이 매우 잘못되었다고 생각하는 부분입니다. –