我写了这段代码:
# -*- coding: utf-8 -*-
import wx
import wx.animate
import wx.lib.agw.hyperlink as hl
ID_BUTTON_ACCEPT = 700
ID_BUTTON_CANCEL = 701
ID_TEXT_INPUT_LOG = 702
ID_TEXT_LABLE_LOG = 703
ID_TEXT_INPUT_PSW = 704
ID_TEXT_LABEL_PSW = 705
ID_NEW_MEMBER_TXT = 706
ID_PRELOADER = 707
class TextObjectValidator(wx.PyValidator):
def __init__(self):
wx.PyValidator.__init__(self)
def Clone(self):
return TextObjectValidator()
def Validate(self, win):
textCtrl = self.GetWindow()
text = textCtrl.GetValue()
if len(text) == 0:
wx.MessageBox("A text object must contain some text!", "Error")
textCtrl.SetBackgroundColour("pink")
textCtrl.SetFocus()
textCtrl.Refresh()
return False
else:
textCtrl.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
textCtrl.Refresh()
return True
def TransferToWindow(self):
return True
def TransferFromWindow(self):
return True
class CloudStorage(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, -1, title, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
# inputs
self.login_label = wx.StaticText(self, ID_TEXT_LABLE_LOG, label='Логин', pos=(15, 15))
self.login_input = wx.TextCtrl(self, ID_TEXT_INPUT_LOG, "", size=(210, -1), pos=(75, 10),
validator = TextObjectValidator())
self.pass_label = wx.StaticText(self, ID_TEXT_LABEL_PSW, label='Пароль', pos=(15, 55))
self.pass_input = wx.TextCtrl(self, ID_TEXT_INPUT_PSW, size=(210, -1), pos=(75, 50), style=wx.TE_PASSWORD,
validator = TextObjectValidator())
# buttons
self.accept_button = wx.Button(self, id=ID_BUTTON_ACCEPT, label='Войти', pos=(15, 95))
self.accept_button.SetBackgroundColour('#BFD8DF')
self.accept_button.SetForegroundColour("#2F4D57")
self.accept_button.SetFont((wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0)))
self.cancel_button = wx.Button(self, id=ID_BUTTON_CANCEL, label='Отмена', pos=(110, 95))
# hyperlink to register
self.new_member_label = hl.HyperLinkCtrl(self, ID_NEW_MEMBER_TXT, 'Зарегистрироваться...', pos=(15,130))
self.new_member_label.EnableRollover(True)
self.new_member_label.SetUnderlines(False, False, True)
self.new_member_label.AutoBrowse(False)
self.new_member_label.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0))
# preloader
self.preloader = wx.animate.AnimationCtrl(self, ID_PRELOADER, pos=(260, 97), size=(24, 24))
self.preloader.LoadFile('./gui/icons/preloader.gif', wx.animate.ANIMATION_TYPE_GIF)
self.preloader.Hide()
# events
self.Bind(wx.EVT_BUTTON, self.OnRegisterNewUser, id=ID_BUTTON_ACCEPT)
self.Bind(hl.EVT_HYPERLINK_LEFT, self.OnRegisterNewUser, id=ID_NEW_MEMBER_TXT)
self.Bind(wx.EVT_BUTTON, self.OnExit, id=ID_BUTTON_CANCEL)
# form settings
size = (310, 150)
self.SetSize(size)
self.icon = wx.Icon('./gui/icons/app.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)
self.Show()
def PreloaderPlay(self):
self.preloader.Show()
self.preloader.Play()
def PreloaderStop(self):
self.preloader.Hide()
self.preloader.Stop()
def OnExit(self, event):
self.Close()
def OnRegisterNewUser(self, event):
self.Hide()
def OnExitRegister(self, event):
self.Close()
def OnExitFilemanager(self, event):
self.Close()
if __name__ =='__main__':
app = wx.App(0)
frame = CloudStorage(None, -1, 'Авторизация')
frame.Show()
app.MainLoop()
并希望在按下“接受”按钮后验证输入的数据(俄语是“Войти”),但没有任何反应。但是,验证器将在TextCtrl的构造函数中使用。怎么了?
答案 0 :(得分:2)
我对wxPyvalidators进行了一些挖掘,并发现了一个早期的SO问题:wx.TextCtrl and wx.Validator。
在class CloudStorage(wx.Frame)
中将此行添加到:
def OnRegisterNewUser(self, event):
if self.login_input.GetValidator().Validate(self.login_input) and self.pass_input.GetValidator().Validate(self.pass_input):
self.Hide()
如果您使用的是wx.Dialog,显然会自动调用验证器。但是,如果您使用的是wx.Panel或wx.Frame,则必须从事件处理程序中调用validator方法。您可能还想阅读this。我的理解可能是错的。但是代码似乎有效。