Hi,
I’m trying to run a progress bar from a thread, then when the gauge is filled, a final panel should be displayed on frame. But I couldn’t get the final panel displayed.
The procedure is as follows:
- Main frame is displayed
- Main panel is displayed on main frame. This panel contains a button.
- When user presses the button, main panel is hidden and progress panel is shown on main frame and progress thread starts. There is a gauge on this panel. The thread updates the gauge.
- When the gauge is filled, progress panel should be hidden and a final panel should be displayed on main frame
But I couldn’t get the final panel displayed. What’s wrong in the following code? Thanks, Best Regards.
import time
import wx
import wx.lib.agw.pygauge as PG
from pubsub import pub
from threading import Thread
class FinalPanel(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent, style=wx.BORDER_SUNKEN)
self.frame = parent
btn2 = wx.Button(self, label="Final Panel", pos=(20, 40))
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer1.Add(btn2, 0, wx.ALIGN_CENTER)
self.SetSizer(sizer1)
class ProgressPanel(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent, style=wx.BORDER_SUNKEN)
self.frame = parent
self.gauge = PG.PyGauge(self, -1, size=(400, 30), style=wx.GA_HORIZONTAL)
self.gauge.SetBarColour("blue")
self.gauge.SetBackgroundColour(wx.Colour(135,135,135))
gauge_text_font = wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, underline=False, faceName="Corbel")
self.gauge.SetDrawValue(True,True,gauge_text_font, wx.Colour(201, 12, 93))
self. update_gauge_range(None, 200)
pub.subscribe(self.updateProgress, "update")
sizer2 = wx.BoxSizer(wx.VERTICAL)
sizer2.Add(self.gauge, 0, wx.ALIGN_CENTER)
self.SetSizer(sizer2)
def start_progress(self):
ProgressThread(self.frame)
def update_gauge_range(self, msg, rng=200):
self.range = rng
self.gauge.SetRange(self.range)
self.value = 0
self.gauge.SetValue(self.value)
self.gauge.Refresh()
self.Refresh()
def updateProgress(self):
self.value += 1
if self.value <= self.range:
self.gauge.SetValue(self.value)
self.gauge.Refresh()
self.Refresh()
else:
pass
class ProgressThread(Thread):
def __init__(self, main_frame):
Thread.__init__(self)
self.main_frame = main_frame
self.start() # start the thread
def run(self):
for i in range(200):
print("i: ", i)
time.sleep(0.01)
pub.sendMessage("update")
pub.sendMessage("show_main_panel")
class MainPanel(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent, style=wx.BORDER_SUNKEN)
self.frame = parent
btn2 = wx.Button(self, label="Start Progress", pos=(20, 40))
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer1.Add(btn2, 0, wx.ALIGN_CENTER)
self.SetSizer(sizer1)
pub.subscribe(self.show_main_panel, "show_main_panel")
btn2.Bind(wx.EVT_BUTTON, self.OnShowProgressPanel)
def OnShowProgressPanel(self, evt):
self.Hide()
self.frame.progress_panel = ProgressPanel(self.frame)
self.frame.sizer.Add(self.frame.progress_panel, 1, wx.EXPAND)
self.frame.sizer.Layout()
self.frame.progress_panel.start_progress()
def show_main_panel(self):
self.frame.progress_panel.Hide()
self.frame.final_panel = FinalPanel(self.frame)
self.frame.sizer.Add(self.frame.final_panel, 1, wx.EXPAND)
self.frame.sizer.Layout()
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Frame",
style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX,
size=wx.Size(580, 700))
self.main_panel = MainPanel(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.main_panel, 1, wx.EXPAND)
self.SetSizer(self.sizer)
self.Layout()
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()