我有一个 TextCtrl,它有一个 EVT_KILL_FOCUS 事件,我用它来验证字段的内容,当值错误时提醒用户。打开 MessageBox 后,我清除该字段并将焦点设置到我再次验证的字段。问题是应该出现在字段内的文本闪烁光标消失了,我不知道为什么或如何修复它。此行为导致用户不知道焦点位于哪个字段中。
有没有人有任何想法?
...
self.txtCode = wx.TextCtrl(self, value='')
self.txtCode.Bind(wx.EVT_KILL_FOCUS, self.__onTxtCodeKillFocus)
self.txtCode.Bind(wx.EVT_CHAR_HOOK, self.__onTxtCodeTabKey)
def __validateTxtCodeContent(self):
if self.txtCode.GetValue() == "":
self.MessageBox(self, "Error Text", _("Warning"))
return False
return True
def __onTxtCodeKillFocus(self, event):
event.Skip()
if self.__validateTxtCodeContent() == False:
self.txtCode.SetValue("")
self.txtCode.SetFocus()
def __onTxtCodeTabKey(self, event):
key = event.GetKeyCode()
shift = event.ShiftDown()
# 9 = TAB, 13 = ENTER
if key != 9 and key != 13:
event.Skip()
return
elif key == 9:
if self.__validateTxtCodeContent():
if shift:
self.btnSave.SetFocus()
else:
self.txtDescription.SetFocus()
else:
self.txtCode.SetValue("")
self.txtCode.SetFocus()
else:
return False
我的验证不仅适用于空字段,而且例如只有空字段可以。
重要提示:在 EVT_CHAR_HOOK 事件中,也会发生此行为。
我也尝试使用它:
self.txtCode.SetValue("")
self.txtCode.SetFocus()
self.txtCode.SetInsertionPointEnd()
self.txtCode.Refresh()
但效果并不好。