Automate Tabbing though tabs in WXnotebook

328 Views Asked by At

I have a WXnotebook that has different number of tabs depending on the amount of information that the program pulls. My goal is to take a screenshot of the information displayed by each tab and store those images. Im having a problem with the program going through the tabs. I was thinking maybe something like

         for i in range(numOfTabs):
            self.waferTab.ChangeSelection(i)
            time.sleep(3)

but this only shows me the last tab in the wxnotebook. If anybody knows anyway of getting this i'd really appreciate it.

EDIT

so I tried the following like suggested below but the GUI shows up but when it shows up It looks like it already iterated through the whole loop and displays the selection is the last tab I still cant see the screen actually going through the tabs

          for i in range(numOfTabs):
            self.waferTab.SetSelection(i)
            Refresh
            wx.SafeYield()
            time.sleep(10)
1

There are 1 best solutions below

0
Mike Driscoll On

I don't know why you would want to do this as it seems like a confusing interface for a user to use, but here's an example using a wx.Timer:

import random
import wx


class TabPanel(wx.Panel):

    def __init__(self, parent):
        """"""
        wx.Panel.__init__(self, parent=parent)

        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))

        btn = wx.Button(self, label="Press Me")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL, 10)
        self.SetSizer(sizer)


class DemoFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """

    def __init__(self):
        """Constructor"""        
        wx.Frame.__init__(self, None, wx.ID_ANY, 
                          "Notebook Tutorial",
                          size=(600,400)
                          )
        panel = wx.Panel(self)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.change_tabs, self.timer)
        self.timer.Start(1000)

        self.notebook = wx.Notebook(panel)
        tabOne = TabPanel(self.notebook)
        self.notebook.AddPage(tabOne, "Tab 1")

        tabTwo = TabPanel(self.notebook)
        self.notebook.AddPage(tabTwo, "Tab 2")

        tabThree = TabPanel(self.notebook)
        self.notebook.AddPage(tabThree, 'Tab 3')

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        self.Layout()

        self.Show()

    def change_tabs(self, event):
        current_selection = self.notebook.GetSelection()
        print(current_selection)
        pages = self.notebook.GetPageCount()
        if current_selection + 1 == pages:
            self.notebook.ChangeSelection(0)
        else:
            self.notebook.ChangeSelection(current_selection + 1)


if __name__ == "__main__":
    app = wx.App(True)
    frame = DemoFrame()
    app.MainLoop()

You could also use a Thread and use something like wx.CallAfter to update your UI, but I think a timer makes more sense in this case.