5

Is there anyway to disable a notebook tab? Like you can with the Widgets themselves? I have a long process I kick off, and while it should be pretty self-explanatory for those looking at it, I want to be able to prevent the user from mucking around in other tabs until the process it is running is complete.

I couldn't seem to find anything in wx.Notebook to help with this?

Code snippet:

def __init__(self, parent):
    wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT)

    self.AddPage(launchTab.LaunchPanel(self), "Launch")
    self.AddPage(scanTab.ScanPanel(self), "Scan")
    self.AddPage(extractTab.ExtractPanel(self), "Extract")
    self.AddPage(virtualsTab.VirtualsPanel(self), "Virtuals")
agf
  • 171,228
  • 44
  • 289
  • 238
chow
  • 484
  • 2
  • 8
  • 21

3 Answers3

3

It si not doable with wx.Notebook. But you can use some of the more advanced widgets such as wx.lib.agw.aui.AuiNotebook:

import wx
import wx.lib.agw.aui as aui

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        style = aui.AUI_NB_DEFAULT_STYLE ^ aui.AUI_NB_CLOSE_ON_ACTIVE_TAB
        self.notebook = aui.AuiNotebook(self, agwStyle=style)      

        self.panel1 = wx.Panel(self.notebook)
        self.panel2 = wx.Panel(self.notebook)
        self.panel3 = wx.Panel(self.notebook)

        self.notebook.AddPage(self.panel1, "First")
        self.notebook.AddPage(self.panel2, "Second")
        self.notebook.AddPage(self.panel3, "Third")

        self.notebook.EnableTab(1, False)

        self.Show()


app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Fenikso
  • 9,251
  • 5
  • 44
  • 72
  • Does work, but stylistically, it creates other problems for me (the `aui.AuiNotebook` and `wx.FlatNotebook`). Sticking with a regular old wx.Notebook in the end. Thanks though. – chow Aug 20 '11 at 14:56
2

Technically, wx.Notebook doesn't have a way to disable a tab. However, you can do the same thing by checking which tab is clicked on and if it is "disabled", veto the EVT_NOTEBOOK_PAGE_CHANGING or EVT_NOTEBOOK_PAGE_CHANGED event. Alternately, you can use the AUI notebook as mentioned above. Note that that is the one from the agw lib, NOT from the wx.aui one. The FlatNotebook also provides the ability to disable tabs. See the wxPython demo for an example.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
0
class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        p = wx.Panel(self)
        self.nb = wx.Notebook(p)
        ......

        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)

    def OnPageChanged(self, event):
        if wx.IsBusy():
            self.Unbind(wx.EVT_NOTEBOOK_PAGE_CHANGED)
            self.nb.SetSelection(event.GetOldSelection())
            self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)

The active tab can be set by Notebook.SetSelection(). But the event should be unbinded/disable and bind/enable around it to avoid in infinite looping. There should be wx.BeginBusyCursor(), wx.EndBusyCursor() in panel codes. Then The tab changing is "disabled" when app is busy.

aGuegu
  • 1,813
  • 1
  • 21
  • 22
  • I had the same problem with wx.Listbook and used aGuegu answer but in order to avoid recursion, instead of SetSelection and unbinding and binding, simple used ChangeSelection, which does not generate the page changing events. – debihiga Nov 12 '15 at 17:43