views:

135

answers:

2

Ok guys, I'm really new at python (and programming itself) so sorry for my ignorance, but I really needed to ask this. So im doing a wxPython project where I added several tabs for a notebook (each tab of the notebook = a class) and there is one tab where I added a checkbox (in a tab, lets call it for example Tab1), and what I want is that when someone checks it, a button that exists in other tab (class called for example tab2) gets hidden where previously it was being shown.

Well I see that it isn't hard to accomplish this, but my problem is the classes (tab1 and tab2, in this example). I've been trying to figure it out by searching but I guess im not searching hard enough because I just can't get it right. If they were in the same class I wouldn't have a problem, but as they are in different classes, im having a huge struggle with this.

Hope someone can help me, and sorry for my ignorance once again.

EDIT: Sorry guys wasn't being shown/hidden, but rather being enabled/disabled.

class Tab2(wx.Panel):
    def __init__(self, parent):
    .....
        self.jaddbutton = wx.Button(self,-1, label ="Button", size = (160,24))
        self.jaddbutton.Bind(wx.EVT_BUTTON, self.jaddbuttonclick, self.jaddbutton)
    def jaddbuttonclick(self, event):
        ....
class Tab1(wx.Panel):
    def __init__(self, parent):
        self.jdcheck = wx.CheckBox(self, -1, 'Disable')
        self.jdcheck.Bind(wx.EVT_CHECKBOX, self.checkoptions, self.jdcheck)
    def checkoptions(self,event):
        checkboxval = self.jdcheck.GetValue()
        if checkboxval == False:
            self.jaddbutton.Disable() # This is what I want to do but it is on the other class

        else:
            self.jaddbutton.Enable() # Same as above

class TextFrame(wx.Frame):
   def __init__(self):
       p = wx.Panel(self)
       self.nb = wx.Notebook(p, size = (750, 332))
       #Tabs
       tab1 = Tab1(self.nb)
       tab2 = Tab2(self.nb)
       self.nb.AddPage(tab1, "ssomething")
       self.nb.AddPage(tab2, "somethingr")
+4  A: 

This sounds more like a wxpython question than a classes question. Normally, in python, tab1 would need a handle to tab2 in order to hide the button in tab2. Or it would need a handle to some shared resource, like a parent class or shared model class, that would allow tab1 to affect settings in tab2 (like the hiding of a button). PyQt provides an event system to allow communication between classes which may not necessarily contain handles to each other. What is the common "accepted" way to communicate in wxpython?

Here is a fairly abstract example of the shared parent solution.

class Parent(object):

    def create_tabs():
        self.tab1 = Tab1(self)
        self.tab2 = Tab2(self)

    def hide_tab2_button():
        self.tab2.hide_button()


class Tab1(object):
    def __init__(self, parent):
        self.parent = parent

    def on_checkbox_checked(self):
        self.parent.hide_tab2_button()


class Tab2(object):
    def __init__(self, parent):
        self.parent = parent

    def hide_button(self):
       self.button.hide() # Or whatever the wxpython command is to hide a button.
Brendan Abel
Thanks man you helped me alot!
Francisco Aleixo
@Francisco, your welcome!
Brendan Abel
+3  A: 

In the tabs' __init__, save the parent reference (the notebook):

class Tab1(wx.Panel):
    def __init__(self, parent):
        self.parent = parent
        ...etc, etc...

Then, self.parent.GetPage(x) lets you access the x-th page (i.e., tab) of the notebook from any other page (tab). So instead of self.jaddbutton.Disable() etc, you'd be using, e.g.:

othertab = self.parent.GetPage(1)
othertab.jaddbutton.Disable()

and so forth.

Alex Martelli
Hey, man thanks alot! You solved literally my problem! Thanks again for your cooperation!
Francisco Aleixo
@Francisco, always glad to be of help!
Alex Martelli
@Francisco: welcome to SO. It's customary to upvote useful answers (click upward-facing arrow).
Adam Bernier
@Adam, you need 15+ rep for that, and Francisco doesn't have it yet.
Alex Martelli
Yeah, haha I actually tried, didn't know I couldn't the first time I tried. I can now though.
Francisco Aleixo