Please see this example below.
In this example, I have an initial panel on my frame, and a start button on it. When I press the button, a progress bar should be displayed on initial panel, then a new thread is started. This new thread creates a new panel and add a notebook with pages on it to new panel. At the same time the progress bar should be updated. After it is done, the initial panel with progress bar will be hidden, and new panel will be placed on the frame. But I can't get it work. Could you please help?
import sys
import wx
import wx.lib.agw.aui as agw_aui
import wx.lib.agw.flatnotebook as fnb
import threading
from wx.lib.buttons import ThemedGenBitmapTextButton
import wx.lib.agw.pygauge as PG
szflags = wx.EXPAND | wx.ALL
min_height = 50
height_ratio = 4
pborder = 10
lborder = 5
class NotebookPage(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
class Notebook(fnb.FlatNotebook):
def __init__(self, *args, **kwargs):
self.gauge = kwargs.pop('gauge', None)
fnb.FlatNotebook.__init__(self, *args, **kwargs)
self.SetBackgroundColour(wx.Colour(226,226,226))
self.nbPages = {}
pages = ["a","b","c"]
for page in pages:
self.nbPages[page] = NotebookPage(self, name='NotebookPage0')
self.gauge.SetValue(self.gauge.GetValue()+10)
for page in pages:
self.AddPage(self.nbPages[page], page, True)
class MainPanel(wx.Panel):
def __init__(self, *args, **kwargs):
self.gauge = kwargs.pop('gauge', None)
wx.Panel.__init__(self, *args, **kwargs)
bookStyle = agw_aui.AUI_NB_DEFAULT_STYLE
bookStyle &= ~(agw_aui.AUI_NB_CLOSE_ON_ACTIVE_TAB)
self.noteBook = Notebook(self, name='Notebook', agwStyle=fnb.FNB_NO_NAV_BUTTONS | fnb.FNB_NODRAG | fnb.FNB_NO_X_BUTTON |fnb.FNB_VC8|fnb.FNB_BOTTOM, gauge=self.gauge)
self.gauge.SetValue(self.gauge.GetValue()+10)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.noteBook, 1, szflags)
self.SetSizer(sizer)
class InitialPanel(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
self.font_box_title = wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD, underline=False, faceName="Microsoft Sans Serif")
self.new_button = ThemedGenBitmapTextButton(self, -1, wx.ArtProvider.GetBitmap(wx.ART_PLUS), label="Start Gauge and add widgets", pos=(-1,-1), size=(320,128))
self.new_button.SetFont(self.font_box_title)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add((20,150))
self.sizer.Add(self.new_button, 0, wx.LEFT, 100)
self.SetSizer(self.sizer)
def bindStartButton(self, mypanel):
self.Bind(wx.EVT_BUTTON, lambda event: self.StartButton(event, mypanel), self.new_button)
def StartButton(self, event, panel):
panel.do_it()
def onOpen(self, event):
self.create_pyprogress()
def create_pyprogress(self):
self.gauge = PG.PyGauge(self, -1, size=(400,40),style=wx.GA_HORIZONTAL)
self.gauge.SetRange(100)
self.gauge.SetBackgroundColour(wx.WHITE)
self.gauge.SetBorderColor(wx.BLACK)
self.keepGoing = True
self.gauge.SetValue(self.gauge.GetValue()+10)
self.sizer.Add((20,40))
self.sizer.Add(self.gauge, 0, wx.LEFT, 100)
self.Layout()
class MainFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.initial_panel = InitialPanel(self)
self.initial_panel.bindStartButton(self)
self.temp_panel = wx.Panel(self)
self.temp_panel.Hide()
self.sizer.Add(self.initial_panel, 1, szflags)
self.SetSizer(self.sizer)
def do_it(self):
self.create_pyprogress()
print "ok"
self.updater = ProgressThread(self)
self.Layout()
def create_pyprogress(self):
self.gauge = PG.PyGauge(self.initial_panel, -1, size=(300,30),style=wx.GA_HORIZONTAL)
self.gauge.SetRange(100)
self.gauge.SetBackgroundColour(wx.WHITE)
self.gauge.SetBorderColor(wx.BLACK)
self.initial_panel.sizer.Add((10,10))
self.initial_panel.sizer.Add(self.gauge, 0, wx.LEFT, 10)
self.initial_panel.Layout()
self.initial_panel.Refresh()
self.initial_panel.Layout()
self.Layout()
def layout_main(self):
self.mainPanel = MainPanel(self.temp_panel, name='MainPanel', gauge= self.gauge)
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.sizer.Hide(self.initial_panel)
self.sizer.Remove(self.initial_panel)
self.sizer.Add(self.mainPanel, 1, szflags)
self.mainPanel.Reparent(self)
self.Layout()
def onExit(self, event):
""""""
self.Close()
sys.exit(1)
def OnClose(self, event):
self.Destroy()
sys.exit(1)
class ProgressThread(threading.Thread):
def __init__(self, panel):
self.main_panel = panel
threading.Thread.__init__(self)
self.start()
def run(self):
self.main_panel.layout_main()
class App(wx.App):
def OnInit(self):
self.mainFrame = MainFrame(None,
size=wx.Size(1000,700),
title='MyFrame', name='MyFrame')
wx.GetApp().SetTopWindow( self.mainFrame )
self.mainFrame.Show()
self.mainFrame.SetSize((500,400))
return True
def main(*args, **kwargs):
global app
app = App()
app.MainLoop()
if __name__ == "__main__":
app = None
main()