wxpython 图片显示切换

mythopoet 发布于 2013/09/04 10:02
阅读 2K+
收藏 0

新手求助,想用wxpython展示2张图片,有两个按钮,按哪个按钮就展展示对应的图片,初始化时加载图片没问题,但是按按钮时无法更新图片显示,希望高手能帮忙解答。代码如下:

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()

加载中
返回顶部
顶部