문제
Python -Tkinter -Entry Widget- validAteCommand (아래)를 사용하면 문자열이 처음 발생할 때 체크가 발생하지만 텍스트를 계속 입력 할 때 확인 단계를 계속 입력하면 처음으로 삭제 또는 삽입이 없습니까? 어떤 충고? (Python을 통해 데스크탑 앱을 구축하지 않는 것 외에)
#!/usr/bin/env python
from Tkinter import *
class MyEntry(Entry):
def __init__(self, master, maxchars):
Entry.__init__(self, master, validate = "key", validatecommand=self.validatecommand)
self.MAX = maxchars
def validatecommand(self, *args):
if len(self.get()) >= self.MAX:
self.delete(0,3)
self.insert(0, "no")
return True
if __name__ == '__main__':
tkmain = Tk()
e = MyEntry(tkmain, 5)
e.grid()
tkmain.mainloop()
해결책
ValidateCommand 또는 InvalidCommand 내에서 Entry Widget을 편집 할 때 Validate 옵션은 또한 없음으로 설정됩니다. 이러한 판은 검증 된 판을 무시합니다. 유효성 검사 중에 입력 위젯 (예 : {}로 설정)을 편집하려면 여전히 유효성 검사 옵션 세트가 있으려면 명령을 포함시켜야합니다.
유휴 후 { %w config -validate %v}
그것을 파이썬으로 번역하는 방법을 모릅니다.
다른 팁
다음은 입력을 5 자로 제한하는 코드 샘플입니다.
import Tkinter as tk
master = tk.Tk()
def callback():
print e.get()
def val(i):
print "validating"
print i
if int(i) > 4:
print "False"
return False
return True
vcmd = (master.register(val), '%i')
e = tk.Entry(master, validate="key", validatecommand=vcmd)
e.pack()
b = tk.Button(master, text="OK", command=lambda: callback())
b.pack()
tk.mainloop()
나는 당신이 콘솔에서 무엇을하고 있는지 볼 수 있도록 많은 인쇄문을 던졌습니다.
당신이 통과 할 수있는 다른 대체물은 다음과 같습니다.
%d Type of action: 1 for insert, 0 for delete, or -1 for focus,
forced or textvariable validation.
%i Index of char string to be inserted/deleted, if any, otherwise -1.
%P The value of the entry if the edit is allowed. If you are config-
uring the entry widget to have a new textvariable, this will be
the value of that textvariable.
%s The current value of entry prior to editing.
%S The text string being inserted/deleted, if any, {} otherwise.
%v The type of validation currently set.
%V The type of validation that triggered the callback (key, focusin,
focusout, forced).
%W The name of the entry widget.
나는 그 이유가 무엇인지 정확히 확신하지만 직감이 있습니다. 유효성 검사는 항목을 편집 할 때마다 수행됩니다. 나는 약간의 테스트를했고 그것이 실제로 실행되는 것을 발견했으며 매번 유효성 검사 중에 모든 종류의 일을 할 수 있습니다. 올바르게 작동을 멈추게하는 원인은 ValidAteCommand 함수 내에서 편집 할 때입니다. 이로 인해 더 이상 Validate 기능 호출을 중단합니다. 더 이상 입력 값이나 무언가에 대한 추가 편집을 인식하지 못한다고 생각합니다.
Lgal Serban은 이것이 왜 발생하는지에 대한 비하인드 정보를 가지고있는 것 같습니다.