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

  • Home
  • SEARCH
  • 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 6752283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:01:35+00:00 2026-05-26T13:01:35+00:00

I’m working on a simple program that collects and checks user-input. In addition to

  • 0

I’m working on a simple program that collects and checks user-input. In addition to displaying
a message box if the user-supplied input fails the checks, I’d also like to add a prompt
just to the right of the input field telling the user what type of data is required. To accomplish this I’ve created a single-row FlexGridSizer consisting of the following:

[(wx.StaticText, “Name”), (wx.TextCtrl, “user inputs here”), (wx.StaticText, “Input Guidance”)]

On initialization the “Input Guidance” widget is hidden. If the user fails to enter anything in provided field, and then hits the OK button, I want three things to happen

  1. Field changes colour to alert user to a problem
  2. The “input guidance” widget (the third in the above row) becomes visible
  3. The DialogBox is automatically resized to take account of the now visible widget

So far I can only get (1) to work, and am looking for help with (2) and (3).


import wx


class Not_Empty(wx.PyValidator):

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

    def Clone(self):

        return Not_Empty()

    ################################################################################
    def Validate(self, win):
        """"""
        evt_location = self.GetWindow()
        val = evt_location.GetValue()

        if val == "":
            evt_location.SetBackgroundColour(wx.Color(250,200,230))
            MyDialog().nameWarning_ST.Show(True)
            return False

        else:
            return True
    ################################################################################

    def TransferToWindow(self):
        return True

    def TransferFromWindow(self):
        return True

class MyDialog(wx.Dialog):
    def __init__(self):
        wx.Dialog.__init__(self, parent=None, id=-1, title="Getting Input", style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)


        # Create field labels
        name = wx.StaticText(self, -1, "Name")

        # Create user-input widgets
        name_TC = wx.TextCtrl(self, validator=Not_Empty())

        # Create and hide warning boxes
        self.nameWarning_ST = wx.StaticText(self, label="Field cannot be left empty", name="emptyAlert")

        # to see what I want the dialog to look like AFTER the user has entered an
        # empty string change 'False' to 'True' in the line below.
        self.nameWarning_ST.Show(False)

        # Create accept/cancel buttons
        btns = self.CreateButtonSizer(flags=wx.OK|wx.CANCEL)

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        fgs = wx.FlexGridSizer(cols = 3, rows = 1)

        fgs.AddMany([(name, -1, wx.ALL, 5), (name_TC, -1, wx.ALL, 5), (self.nameWarning_ST, -1, wx.ALL, 5)])

        self.mainSizer.AddMany([(fgs, 1, wx.ALL, 0), (btns, 1, wx.ALL|wx.EXPAND, 5)])

        self.SetSizer(self.mainSizer)       
        self.mainSizer.Fit(self)

if __name__ == '__main__':

    app = wx.App() 
    dlg = MyDialog()
    dlg.Center()
    dlg.ShowModal()
    dlg.Destroy()

    app.MainLoop()
  • 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-26T13:01:35+00:00Added an answer on May 26, 2026 at 1:01 pm

    EDIT: For the new question(s), I would use pubsub to solve this. Put a listener the init of the MyDialog class and then publish a message to it when the Validate method is run in the other class. In the message handler that you will theoretically create in MyDialog, you’ll want Show your other widget and call Layout() on the dialog. That should cause it to resize appropriately.

    You can read a tutorial on pubsub here: http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/

    The following was for the original question:

    Your Validate method doesn’t ever fire, so I rearranged your Validate and OnChar methods to look like the following:

    import string
    def Validate(self, win):
        """
        Enter your code here...
        """
        return True
    
    
    def OnChar(self, event):
        txtObj = event.GetEventObject()
        txt = txtObj.GetValue()
        if txt == "": txt = '0'
        key = chr(event.GetKeyCode())
    
        if key in string.digits:
            if int(txt+str(key)) < 150:
                event.Skip()
            else:
                msg = "You have entered an age greater than 150!"
                wx.MessageBox(msg, "Improbable entry", style=wx.OK|wx.ICON_ERROR)
    
                # PROBLEM ARISES HERE
                # ===================
                MyDialog().ageWarning_ST.Show()
                MyDialog().mainSizer.Fit(MyDialog())
    
                return False
    
        key = event.GetKeyCode()
        if key in(wx.WXK_RETURN, wx.WXK_DELETE, wx.WXK_BACK):
            event.Skip()
    
        elif (chr(key)).isdigit():
            event.Skip()
        else:
            return
    

    Also note that I imported the string module to makes things a little easier. The code probably needs some clean up, but it worked on my machine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT

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.