gpt4 book ai didi

python - 验证 TextCtrl 中的字符串

转载 作者:行者123 更新时间:2023-12-01 05:08:53 25 4
gpt4 key购买 nike

我写了这段代码:

# -*- 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 的构造函数中使用。怎么了?

最佳答案

我对 wxPyvalidators 进行了一些挖掘,并发现了一个较早的关于此的问题:wx.TextCtrl and wx.Validator .

在您的类 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,则必须从事件处理程序中调用验证器方法。您可能还想阅读this 。但我的理解可能是错误的。不过,代码似乎可以工作。

关于python - 验证 TextCtrl 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24573892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com