How to enable/disable a TextCtrl using a CheckBox

2,953 views
Skip to first unread message

JR

unread,
Dec 9, 2010, 1:55:32 AM12/9/10
to wxPython-users
Hi,
I'm a newbie to wxPython.

I'm trying to enable/disable TextCtrl objects using the status of a
CheckBox object.
However, the binding of TextCtrl object to EVT_CHECKBOX does not work.
The following is the sample code.

Is this a bug or not supposed to work?

Thanks for help in advance.

============================================================
import wx

def OnPanelTest(evt):
print evt.GetEventObject().GetLabel(),
print 'EVT_CHECBOX is bound to a Panel object'
evt.Skip()

def OnTextCtrlTest(evt):
print evt.GetEventObject().GetLabel(),
print 'EVT_CHECBOX is bound to a TextCtrl object'
evt.Skip()

app = wx.PySimpleApp()
frm = wx.Frame(None,-1,"Test")
panel = wx.Panel(frm)
sizer = wx.BoxSizer(wx.HORIZONTAL)

chbox = wx.CheckBox(panel, -1, 'CheckBox')
sizer.Add(chbox, 0, wx.ALL, 10)
ctrl = wx.TextCtrl(panel, -1, 'TextCtrl')
sizer.Add(ctrl, 0, wx.ALL, 10)

panel.Bind(wx.EVT_CHECKBOX, OnPanelTest, chbox) # working
ctrl.Bind(wx.EVT_CHECKBOX, OnTextCtrlTest, chbox) # not working

panel.SetAutoLayout(True)
panel.SetSizer(sizer)
sizer.Fit(panel)
sizer.SetSizeHints(panel)
panel.Layout()
app.SetTopWindow(frm)
frm.Show()
app.MainLoop()

mw

unread,
Dec 9, 2010, 2:26:44 AM12/9/10
to wxpytho...@googlegroups.com
Try this for a good explanation of event propagation and binding:

the wx documentation is also good; wxEvent or Event Handling Overview.
I've attached your code with one correction and a few comments.

HTH
-mark

checkboxtest.py

JR

unread,
Dec 9, 2010, 2:56:48 AM12/9/10
to wxPython-users
Thanks a lot for the code. It works as I wanted.
Now I understand the explanation of event handling.

On Dec 8, 11:26 pm, mw <m...@tangsoo.us> wrote:
> Try this for a good explanation of event propagation and binding:http://wiki.wxpython.org/self.Bind%20vs.%20self.button.Bind
>
>  checkboxtest.py
> 1KViewDownload
> > To unsubscribe, send email to wxPython-user...@googlegroups.com<wxPython-users%2Bunsubscribe@go­oglegroups.com>
> > or visithttp://groups.google.com/group/wxPython-users?hl=en- Hide quoted text -
>
> - Show quoted text -

Boštjan Mejak

unread,
Dec 9, 2010, 3:37:53 AM12/9/10
to wxpytho...@googlegroups.com

To enable or disable a text control, use Enable() or Disable() methods of wx.TextCtrl, respectively.

Dev Player

unread,
Dec 9, 2010, 8:51:33 AM12/9/10
to wxpytho...@googlegroups.com
Alternatively use
wx.TextCtrl.Enable(False)
instead of Disable() to allow for boolean toggle logic
 
or
toggle = chbox.IsChecked() to Enable when checked, disable when unchecked
toggle = not chbox.IsChecked() to disable when checked, enable when unchecked
ctrl.Enable(toggle)
 
or
def OnPanelTest(evt):
    ctrl.Enable( chbox.IsChecked() )
 
I've never tested it and the docs don't say but instead of using "not" you might try .Disable(bool)  [instead of just Disable() or just Enable() if the ctrl enabled state is opposite of chbox Checked state.
 

Boštjan Mejak

unread,
Dec 9, 2010, 10:14:07 AM12/9/10
to wxpytho...@googlegroups.com
What's wrong with wx.TextCtrl.Disable()? It's more Pythonic than wx.TextCtrl.Enable(False).

DevPlayer

unread,
Dec 9, 2010, 10:50:18 AM12/9/10
to wxPython-users
I believe every widget has an Enable(), like menus. I'm not sure every
widget has a Disable() method.

Dev Player

unread,
Dec 9, 2010, 10:54:54 AM12/9/10
to wxpytho...@googlegroups.com
Alternate method attached.
 
cb_enables.py

Robin Dunn

unread,
Dec 9, 2010, 12:15:13 PM12/9/10
to wxpytho...@googlegroups.com
On 12/9/10 7:50 AM, DevPlayer wrote:
> I believe every widget has an Enable(), like menus. I'm not sure every
> widget has a Disable() method.
>

They do. It's inherited from wx.Window just like Enable is.

$ pydoc wx.Window.Disable
Help on method Disable in wx.Window:

wx.Window.Disable = Disable(*args, **kwargs) unbound wx._core.Window method
Disable(self) -> bool

Disables the window, same as Enable(false).

--
Robin Dunn
Software Craftsman
http://wxPython.org

Boštjan Mejak

unread,
Dec 9, 2010, 2:14:24 PM12/9/10
to wxpytho...@googlegroups.com

I propose using the Disable() method.

mw

unread,
Dec 9, 2010, 4:53:14 PM12/9/10
to wxpytho...@googlegroups.com
Menus and MenuItems have no .Disable() option, but do have .Enable(False).

As Robin mentioned, they're not derived from wx.Window, so no inherited .Disable().
The MenuBar itself is derived from wx.Window and does have a .Disable(), but I don't think it actually does anything (someone please correct me if I"m wrong).



On Thu, Dec 9, 2010 at 11:14 AM, Boštjan Mejak <bostja...@gmail.com> wrote:

I propose using the Disable() method.

--
To unsubscribe, send email to wxPython-user...@googlegroups.com

Boštjan Mejak

unread,
Dec 10, 2010, 5:45:03 AM12/10/10
to wxpytho...@googlegroups.com

The classes wx.Menu and wx.MenuItem should also have the .Disable() method. Robin, can you implement it in wxWidgets and thus in wxPython?

C M

unread,
Dec 10, 2010, 11:44:49 AM12/10/10
to wxpytho...@googlegroups.com
On Fri, Dec 10, 2010 at 5:45 AM, Boštjan Mejak <bostja...@gmail.com> wrote:

The classes wx.Menu and wx.MenuItem should also have the .Disable() method. Robin, can you implement it in wxWidgets and thus in wxPython?

If you go to the wxPython.org website and scan the column of text on the left, you will see a link to Feature Requests.  Click on that, you'll be brought to Trac (and yes, it is for wxWidgets and also wxPython), and then read about how to submit a ticket, and then you can submit actual feature requests. 

Mike Driscoll

unread,
Dec 10, 2010, 2:10:26 PM12/10/10
to wxPython-users


On Dec 10, 4:45 am, Boštjan Mejak <bostjan.me...@gmail.com> wrote:
> The classes wx.Menu and wx.MenuItem should also have the .Disable() method.
> Robin, can you implement it in wxWidgets and thus in wxPython?

I recommend switching to one of the many other GUI toolkits out there.
Or a web framework.

Reply all
Reply to author
Forward
0 new messages