How to enable/disable a TextCtrl using a CheckBox

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()

Try this for a good explanation of event propagation and binding:

http://wiki.wxpython.org/self.Bind%20vs.%20self.button.Bind

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 (1.08 KB)

···

On Wed, Dec 8, 2010 at 10:55 PM, JR jhrhew@hotmail.com wrote:

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()

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

or visit http://groups.google.com/group/wxPython-users?hl=en

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

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

On Wed, Dec 8, 2010 at 10:55 PM, JR <jhr...@hotmail.com> wrote:
> 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()

> --
> To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@go­oglegroups.com>
> or visithttp://groups.google.com/group/wxPython-users?hl=en- Hide quoted text -

- Show quoted text -

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

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.

What’s wrong with wx.TextCtrl.Disable()? It’s more Pythonic than wx.TextCtrl.Enable(False).

I believe every widget has an Enable(), like menus. I'm not sure every
widget has a Disable() method.

Alternate method attached.

cb_enables.py (1.86 KB)

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).

···

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.

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

I propose using the Disable() method.

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 bostjan.mejak@gmail.com wrote:

I propose using the Disable() method.

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

or visit http://groups.google.com/group/wxPython-users?hl=en

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.

···

On Fri, Dec 10, 2010 at 5:45 AM, Boštjan Mejak bostjan.mejak@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.

···

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?