我想使用條目控件來獲取1到9之間的數字。如果按下其他任何按鍵,我想將其從顯示中刪除。清除條目控件框
def onKeyPress(event):
if event.char in ['1', '2', '3', '4', '5', '6', '7', '8', '9']
...do something
return
# HERE I TRY AND REMOVE AN INVALID CHARACTER FROM THE SCREEN
# at this point the character is:
# 1) visible on the screen
# 2) held in the event
# 3) NOT YET in the entry widgets string
# as the following code shows...
print ">>>>", event.char, ">>>>", self._entry.get()
# it appeARS that the entry widget string buffer is always 1 character behind the event handler
# so the following code WILL NOT remove it from the screen...
self._entry.delete(0, END)
self._entry.insert(0, " ")
# here i bind the event handler
self._entry.bind('<Key>', onKeyPress)
好的,我該如何清除屏幕?
在這種特殊情況下,我建議你刪除所有那些額外的參數,因爲你不使用它們。爲了說明的目的,我把它們放在其他答案中,但是在這裏你並沒有使用它們,所以它們只能使解決方案比需要的更復雜。 –
hanks Bryan和Jdog,我會嘗試這個驗證碼。如果可能,我想保留OnKeyPress事件處理程序 - 如果輸入有效,我實際上使用它來移動到另一個輸入小部件;所以一個後續問題 - 你可以使用驗證和事件處理程序來完成任務嗎?如果你可以首先驗證或事件處理程序? – paddypantsonfire