wilbev
wilbev

Reputation: 5991

Dynamically Disable Checkbox in wxpython

So I have a wxpython app where I would like to disable a checkbox widget when another checkbox is changed to false by the user. I currently use following code to set these two checkboxes to true.

self.parentCheckbox.SetValue(True)
self.childCheckbox.SetValue(True)

Now I want to disable the childCheckbox since it's no longer relevant if the user makes the parentCheckbox false. I was hoping the following code would do this but it doesn't seem to be doing anything.

if self.parentCheckbox.GetValue() == False:
    self.childCheckbox.Disabled()    

Upvotes: 0

Views: 1576

Answers (2)

vincent-lg
vincent-lg

Reputation: 559

To be sure, since it seems no one pointed that out: your code was okay except for one tiny letter.

self.childCheckbox.Disabled()    

Instead the correct method name, of course, is Disable:

self.childCheckbox.Disable()

(Without the D: this time it's the action, not the test.)

The accepted answer seemed to integrate that but offered it mingled with a lot of code that wasn't (in my opinion) necessary to show you the error.

Hope that helps!

Upvotes: 1

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

It's rough and ready but it shows how it can be done without pubsub.

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "My Frame", size=(3000, 3000))
        self.panel = wx.Panel(self,-1)
        self.a = wx.CheckBox(self.panel, -1, "Apples", (20,100), (160,-1))
        self.b = wx.CheckBox(self.panel, -1, "Mango", (20,150), (160,-1))
        self.c = wx.CheckBox(self.panel, -1, "Banana", (20,200), (160,-1))
        self.d = wx.CheckBox(self.panel, -1, "Orange", (20,250), (160,-1))
        button=wx.Button(self.panel,label="Child",pos=(800, 400), size = (50,50))
        self.Bind(wx.EVT_BUTTON, self.newwindow, button)
        self.a.SetValue(True)

    def newwindow(self, event):
        secondWindow = window2(parent=self)
        secondWindow.Show()


class window2(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent, -1,'Window2', size=(1000,700))
        self.parent = parent
        self.panel=wx.Panel(self, -1)
        self.chk = wx.CheckBox(self.panel, -1, "Apples", (20,100), (160,-1))
        self.Bind(wx.EVT_CHECKBOX, self.Check)
        self.SetBackgroundColour(wx.Colour(100,100,100))
        self.Centre()
        self.Show()

def Check(self,event):
    if self.chk.IsChecked() == True:
        self.parent.a.SetValue(False)
        self.parent.a.Disable()
    else:
        self.parent.a.SetValue(True)
        self.parent.a.Enable()
    self.parent.Update()

app = wx.App()
frame = MyFrame()
frame.Show(True)
app.MainLoop()

Upvotes: 1

Related Questions