Help:wxpython loading new pictures

Post here if you need help with creating a Graphical User Interface in Python.

Help:wxpython loading new pictures

Postby zclmythopoet » Wed Sep 04, 2013 3:24 am

I am a newbie. I want to show 2 pictures by using wxpython.
There are two buttons, each of which corresponds to one picture. When a button is pressed, the corresponded picture should be shown.
There's no problem loading the picture when the App is started.But I have no idea how to load the new picture when a button is pressed.
Here is the code:
Code: Select all
import wx

class ImageWindow(wx.Window):

    def __init__(self,parent,id):
        wx.Window.__init__(self, parent, id)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.image = None

    def SetImage(self, image):
        self.image = image
        self.Refresh(True)

    def OnPaint(self,evt):
        dc = wx.PaintDC(self)
        if self.image:
            dc.DrawBitmap(self.image.ConvertToBitmap(), 0, 0, False)

class AppFrame(wx.Frame):

    def __init__(self, parent, ID, title, pos=wx.DefaultPosition,size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
        wx.Frame.__init__(self,parent, ID, title, pos, size, style)

        vbox = wx.BoxSizer(wx.VERTICAL)
        topBox = wx.BoxSizer(wx.HORIZONTAL)
        botBox = wx.BoxSizer(wx.HORIZONTAL)
        vbox.Add(topBox, 1, wx.EXPAND)
        vbox.Add(botBox)

        btnA = wx.Button(self,wx.ID_ANY, 'Button A')
        #self.Bind(wx.EVT_BUTTON, self.OnClick, self.btnA)
        btnB = wx.Button(self,wx.ID_ANY, 'Button B')

        botBox.Add(btnA)
        botBox.Add(btnB)

        imw = ImageWindow(self, wx.ID_ANY)
        topBox.Add(imw, 1, wx.EXPAND)

        image = wx.Image('image.png', wx.BITMAP_TYPE_PNG)
        imw.SetImage(image)

        self.SetSizer(vbox)

##    def OnClick(self,evt):
##        image = wx.Image('bbb.png', wx.BITMAP_TYPE_PNG)
##        imw.SetImage(image)

class MyApplication(wx.App):

    def OnInit(self):
        wnd = AppFrame(None, wx.ID_ANY, "Main Window")
        wnd.Show(True)
        return True

def main():
    app = MyApplication(False)
    app.MainLoop()

if __name__ == "__main__":
    main()
Last edited by Mekire on Wed Sep 04, 2013 8:16 am, edited 2 times in total.
Reason: First post lock. Fixed indentation.
zclmythopoet
 
Posts: 2
Joined: Wed Sep 04, 2013 3:02 am

Re: Help:wxpython loading new pictures

Postby Mekire » Wed Sep 04, 2013 8:10 am

Firstly, I'll say I have little experience with wxpython, but...

Basically it boils down to scope issues. You didn't declare your image window an instance variable, so when the init ends it goes out of scope.

This gets things running (though your AppFrame __init__ needs to be cleaned up a lot):
Code: Select all
import wx

class ImageWindow(wx.Window):
    def __init__(self,parent,id):
        wx.Window.__init__(self, parent, id)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.image = None

    def SetImage(self, image):
        self.image = image
        self.Refresh(True)

    def OnPaint(self,evt):
        dc = wx.PaintDC(self)
        if self.image:
            dc.DrawBitmap(self.image.ConvertToBitmap(), 0, 0, False)


class AppFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self,parent, ID)
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.topBox = wx.BoxSizer(wx.HORIZONTAL)
        self.botBox = wx.BoxSizer(wx.HORIZONTAL)
        self.vbox.Add(self.topBox, 1, wx.EXPAND)
        self.vbox.Add(self.botBox)
        self.btnA = wx.Button(self,0, 'Button A')
        self.btnB = wx.Button(self,1, 'Button B')
        self.Bind(wx.EVT_BUTTON, self.OnClick, self.btnA)
        self.Bind(wx.EVT_BUTTON, self.OnClick, self.btnB)
        self.botBox.Add(self.btnA)
        self.botBox.Add(self.btnB)
        self.imw = ImageWindow(self, wx.ID_ANY)
        self.topBox.Add(self.imw, 1, wx.EXPAND)
        self.images = [wx.Image('image.png', wx.BITMAP_TYPE_PNG),
                       wx.Image('bbb.png', wx.BITMAP_TYPE_PNG)]
        self.imw.SetImage(self.images[0])
        self.SetSizer(self.vbox)

    def OnClick(self,evt):
        image = self.images[evt.GetId()]
        self.imw.SetImage(image)


class MyApplication(wx.App):
    def OnInit(self):
        wnd = AppFrame(None, wx.ID_ANY, "Main Window")
        wnd.Show(True)
        return True

def main():
    app = MyApplication(False)
    app.MainLoop()


if __name__ == "__main__":
    main()

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Help:wxpython loading new pictures

Postby zclmythopoet » Wed Sep 04, 2013 1:47 pm

Thanks very much! :)
zclmythopoet
 
Posts: 2
Joined: Wed Sep 04, 2013 3:02 am


Return to GUI

Who is online

Users browsing this forum: Google Feedfetcher and 3 guests