Satya Prakash_1 0 Newbie Poster

I am writing a GUI flow using wxPython which has 4 pages (or more). They way I have approached is creating 4 (or more) classes with each class defining its own static (background) and dynamic images / content. In my application I would then programmatically create instances class required and capture events on that page. Based upon the event triggered the registered handler would destroy current class and switch to other class(page). So my code actually creates X classes with each class having its own method to set background / foreground content/images.Note I have created 2 classes (MainPanel, SecondPanel)that creates a screen on panel in my application frame. It then waits for an event. Once I get the desired event I delete the current class and create an instance of new class:

import wx

########################################################################
class SecondPanel(wx.Panel):
    def __init__(self,parent, image, state):
        wx.Panel.__init__(self, parent=parent)
    self.state = state
    self.image = image
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.frame = parent
        sizer = wx.BoxSizer(wx.VERTICAL)
        hSizer = wx.BoxSizer(wx.HORIZONTAL)
    panel=wx.Panel(self, -1)

    self.buttonOne=wx.Image("image1.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
    self.button=wx.BitmapButton(self, -1, self.buttonOne, pos=(100,50))
    self.button.Bind(wx.EVT_LEFT_DCLICK, self.buttonClick)
        sizer.Add(self.button, 0, wx.ALL, 5)
        hSizer.Add((1,1), 1, wx.EXPAND)
        hSizer.Add(sizer, 0, wx.TOP, 100)
        hSizer.Add((1,1), 0, wx.ALL, 75)
        self.SetSizer(hSizer)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

    def buttonClick(self, evt):
    parent = self.frame
    self.Destroy()
    DispatchState(parent, "admin0.png", 0)

    def OnEraseBackground(self, evt):
    dc = evt.GetDC()

    if not dc:
        dc = wx.ClientDC(self)
        rect = self.GetUpdateRegion().GetBox()
        dc.SetClippingRect(rect)
    dc.Clear()
    bmp = wx.Bitmap(self.image)
    dc.DrawBitmap(bmp, 0, 0)

class MainPanel(wx.Panel):
    def __init__(self,parent, image, state):
        wx.Panel.__init__(self, parent=parent)
    self.state = state
    self.image = image
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.frame = parent
        sizer = wx.BoxSizer(wx.VERTICAL)
        hSizer = wx.BoxSizer(wx.HORIZONTAL)
    panel=wx.Panel(self, -1)
    self.buttonOne=wx.Image("image0.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
    self.button=wx.BitmapButton(self, -1, self.buttonOne, pos=(100,50))
    self.button.Bind(wx.EVT_LEFT_DCLICK, self.buttonClick)
        sizer.Add(self.button, 0, wx.ALL, 5)
        hSizer.Add((1,1), 1, wx.EXPAND)
        hSizer.Add(sizer, 0, wx.TOP, 100)
        hSizer.Add((1,1), 0, wx.ALL, 75)
        self.SetSizer(hSizer)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

    def buttonClick(self, evt):
    parent = self.frame
    self.Destroy()
    DispatchState(parent, "admin1.png", 1)

    def OnEraseBackground(self, evt):
    dc = evt.GetDC()

    if not dc:
        dc = wx.ClientDC(self)
        rect = self.GetUpdateRegion().GetBox()
        dc.SetClippingRect(rect)
    dc.Clear()
    bmp = wx.Bitmap(self.image)
    dc.DrawBitmap(bmp, 0, 0)


 class Main(wx.App):
    def __init__(self, redirect=False, filename=None):
        wx.App.__init__(self, redirect, filename)
        self.frame = wx.Frame(None, size=(800, 480))
        self.state = 0
        self.image = 'admin0.png'

def DispatchState(frame, image, state):
        if state == 0 :
            print image
            print state
            MainPanel(frame, image, state)
        if state == 1 :
            print image
            print state
            SecondPanel(frame, image, state)
        frame.Show()


if __name__ == "__main__":
    app = Main()
    DispatchState(app.frame,app.image, app.state)
    app.MainLoop()

The reason I have selected this approach is that I can easily switch from one state to other such that I can switch to any screen / page. If suppose tomorrow we need to dynamically add / remove more pages - it can be easily done. I would need to create the page (class) and add its state in DispatchState() global method.

But for me currently second screen does not gets rendered at all. Also please comment on my approach - is there any better way I can achieve this - what are the things I should take care or what is erroneous in my code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.