Reputation: 809
I feel like this should be simple, but I can't find anything out there. I have a pretty simple dialog, with two text controls. I then create a OK/CANCEL buttons sizer using the CreateSeparatedButtonSizer method.
The issue is, I would like to try to enable/disable the "OK" button based off of some criteria on the entries in the text controls. In other words, until valid entries are entered into the text controls, I want the OK button to be disabled. I can't seem to find anything on how to reference the button, and I'd rather not manually create the buttons so that the dialog remains platform 'agnostic.'
Small sample code:
class MyDialog(wx.Dialog):
def __init__(self, parent, title):
wx.Dialog.__init__(self, parent=parent, title=title)
# Grid sizer for text controls and labels:
grid = wx.GridBagSizer(2,2)
# Add the input fields:
grid.Add(wx.StaticText(self, label="Field 1: "),pos=(0,0))
self.fld1 = wx.TextCtrl(self, value="", size=(70,-1))
grid.Add(self.fld1, pos=(0,1))
grid.Add(wx.StaticText(self, label="Field 2: "),pos=(1,0))
self.fld2 = wx.TextCtrl(self, value="", size=(70,-1))
grid.Add(self.fld2, pos=(1,1))
# Buttonsizer:
btns = self.CreateSeparatedButtonSizer(wx.OK|wx.CANCEL)
# Lay it all out:
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(grid, 1, wx.ALL|wx.EXPAND)
mainSizer.Add(btns, 0, wx.ALL|wx.EXPAND)
self.SetSizer(mainSizer)
self.Fit()
So, I'd like to bind a method to the text controls, which checks whether or not the inputs are valid. If they are, then the OK button will be enabled, and if not, then it should be disabled. Is there any way to do this?
Thanks!
Upvotes: 2
Views: 1491
Reputation: 141
The OK button has the id wx.ID_OK
. You can try wx.FindWindowById(wx.ID_OK, self)
if you're trying to find it from within your MyDialog
class.
If you're trying to reference the button from outside of the MyDialog
class, you'll want to use the instance of MyDialog
as the second parameter.
ex.
dialog_instance = MyDialog()
ok_button = wx.FindWindowById(wx.ID_OK, dialog_instance)
Here's some documentation on FindWindowById http://xoomer.virgilio.it/infinity77/wxPython/wxFunctions.html#FindWindowById
Upvotes: 4