I have a textentrydialog and my goal is that the user cannot leave the the text entry blank or just empty whitespace. So the dialog should not go away until the user enters some characters in the box or hits cancel. This is what I have:

def OnNewButton(self,event):
        error=1
        while error==1:
            newName = wx.TextEntryDialog(self, 'Question?:','Title')
            newName.SetValue("")
            if newName.ShowModal() == wx.ID_OK:
                if len(newName.GetValue().strip())!=0:
                    error=0
                    self.Destroy()
            elif newName.ShowModal() == wx.ID_CANCEL:
                error=0
            newName.Destroy()

However, this does not work if the user hits cancel. They actually have to hit cancel twice for the dialog to go away. I cannot see why. Or maybe there is just a better way to do this...Any help is greatly appreciated!

From what I can see it looks like it's because you are calling newName.ShowModal() twice in your code.
In the first call to DoModal in your if statement, a dialog is created and if the user presses ok and has entered some text the dialog is destroyed and your while loop breaks out.
If the user presses ok after entering no text, the dialog is destroyed, your while loop reiterates and the first call to ShowModal is called again. OK so far, I assume this is the behaviour you want.

But if the user closes the initial text box with cancel, your code drops to the elif statement where DoModal is called a second time, creating a 2nd dialog.. In the 2nd call, whether the dialog returns via ok or cancel it will be destroyed.
So it is the call to DoModal in the elif that is the problem!

What you need to do is alter your code so that you're making one call to DoModal and then see what value was returned..
So I'm guessing you'd need to do something like this:

def OnNewButton(self,event):
        error=1
        while error==1:
            newName = wx.TextEntryDialog(self, 'Question?:','Title')
            newName.SetValue("")
            retVal = newName.ShowModal()
            if retVal == wx.ID_OK:
                if len(newName.GetValue().strip())!=0:
                    error=0
                    self.Destroy()
            elif retval == wx.ID_CANCEL:
                error=0
            newName.Destroy()

Hope that solves your problem!

Cheers for now,
Jas.

That's it! Perfect! Thank you.

I've just noticed a few more errors in my post, but I've exceeded the 30 min edit window....Doh! edits are in bold:

But if the user closes the initial text box with cancel, your code drops to the elif statement where ShowModal is called a second time, creating a 2nd dialog.. In the 2nd call, whether the dialog returns via ok or cancel it will be destroyed.
So it is the call to ShowModal in the elif that is the problem!

What you need to do is alter your code so that you're making one call to ShowModal and then see what value was returned..

Sorry about the DoModal's in my previous post please ignore them...I meant to put ShowModal!

DoModal() is a Windows C++ dialog call, whereas ShowModal() is the wxPython equivalent!

I can't blame my stupid, fat drummer fingers for that...I think it was my stupid, empty drummer cranium! heh heh :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.