Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6986685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:50:27+00:00 2026-05-27T18:50:27+00:00

I have a simple form with a multi line text control and Ok/Cancel buttons

  • 0

I have a simple form with a multi line text control and Ok/Cancel buttons on it. I want to check whether the text control has text when Ok button is clicked or not. I’m trying to use the wx.Validator for this purpose, but for some reason it is never get called even the extra style wx.WS_EX_VALIDATE_RECURSIVELY is applied to the parent frame. Here is my code:

import wx

class Validator(wx.PyValidator):
    def __init__(self):
        wx.PyValidator.__init__(self)

    def Clone(self):
        return Validator()

    def Validate(self, win):
        txt_ctrl = self.GetWindow()

        if len(txt_ctrl.GetValue()) == 0:
            wx.MessageBox('Please, provide a value.', 'Error')
            txt_ctrl.SetBackgroundColour('pink')
            txt_ctrl.SetFocus()
            txt_ctrl.Refresh()
            return False
        else:
            txt_ctrl.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
            txt_ctrl.Refresh()
            return True

    def TransferToWindow(self):
        return True

    def TransferFromWindow(self):
        return True

class OkCancelPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY)

        self.btn_ok =  wx.Button(self, wx.ID_OK)
        self.btn_cancel = wx.Button(self, wx.ID_CANCEL)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.btn_ok, 0, wx.NORMAL|wx.ALL, 5)
        sizer.Add(self.btn_cancel, 0, wx.NORMAL|wx.ALL, 5)            
        self.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY)

        self.txt = wx.TextCtrl(self, wx.ID_ANY, style = wx.TE_MULTILINE)
        self.okcancel = OkCancelPanel(self)

        sizer_txt = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, 'Some text here'), wx.HORIZONTAL)
        sizer_txt.Add(self.txt, 1, wx.EXPAND, 0)
        sizer_txt.Layout()
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(sizer_txt, 1, wx.EXPAND|wx.ALL, 5)
        sizer.Add(self.okcancel, 0, wx.NORMAL|wx.ALIGN_RIGHT, 0)
        self.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()

class Frame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, wx.ID_ANY, 'Test Frame', size = (400, 300))
        self.SetSizeHints(minW = 400, minH = 300)

        self.panel = Panel(self)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.panel, 1, wx.EXPAND, 0)
        self.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()

        #--Shortcuts--#
        self.txt = self.panel.txt
        self.btn_ok = self.panel.okcancel.btn_ok
        self.btn_cancel = self.panel.okcancel.btn_cancel

class Controller(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.SetExtraStyle(wx.WS_EX_VALIDATE_RECURSIVELY)   

        self.txt.SetValidator(Validator())     

        self.Bind(wx.EVT_BUTTON, self.OnButton, self.btn_ok )

    def OnButton(self, evt):
        self.txt.Validate()

if __name__ == '__main__':
    app = wx.App()
    frame = Controller(None)
    frame.Show()
    app.SetTopWindow(frame)
    app.MainLoop()

So, what I’m doing wrong?

Sure I can use something like self.txt.GetValidator().Validate(self.txt) in the OnButton method, but I don’t want 🙂

Windows 7 x64, Python 2.7.2, wxPython 2.9.2.4

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:50:28+00:00Added an answer on May 27, 2026 at 6:50 pm

    Instead of:

    self.txt.Validate()
    

    use:

    self.Validate()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form on a view page, implemented as a user control,
I have a simple form on a page and I want to auto submit
I have a simple form like this: <form name=serachForm method=post action=/home/search> <input type=text name=searchText
I have large text in System.Windows.Forms.TextBox control in my form (winforms), vs 2008. I
In my windows form I have a text box and a button, the text
I have a simple form as <form method=post action=target.php> <input class=button type=submit value=Submit />
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
Have a simple form (only extract fields here) but for some reason the JQserilization
I have a simple form with some plain html input like bellow using ASP.NET
I have a simple form that looks like so <% remote_form_for post, :url =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.