2015-02-24 73 views
0

我一直在試圖交換列表中的2張圖片。我將這個列表添加到1行2列的GridSizer中。我有一個水平的BoxSizer,我在其中添加了GridSizer以及一個按鈕,點擊時,圖片應該被交換。但我得到了類型錯誤字符串或unicode需要。 我在Linux Mint 64位筆記本電腦上使用Python 2.7.6和wxPython 2.8.12.1(gtk2-unicode)。以下是我的程序中發生錯誤的部分。 請幫忙。wxPython交換圖像

謝謝。

def OnOk(self, event): 
    x = wx.Image(self.ic[0], wx.BITMAP_TYPE_ANY).Scale(200, 200) 
    y = wx.Image(self.ic[1], wx.BITMAP_TYPE_ANY).Scale(200, 200) 
    self.ic[0].SetBitmap(wx.StaticBitmap(self, wx.ID_ANY, wx.BitmapFromImage(y))) 
    self.ic[1].SetBitmap(wx.StaticBitmap(self, wx.ID_ANY, wx.BitmapFromImage(x))) 
    self.Refresh() 
+0

你能提供一個小的可運行的例子嗎? – 2015-02-24 15:56:14

回答

1

我十分不明白self.ic應該是(wx.StaticBitmap實例的列表或wx.Bitmap實例的列表)。你似乎混淆了兩者。 A StaticBitmapwxPython小部件,wx.Bitmap就是一個保存位圖數據的數據結構。

見下文一個工作例如:

import wx 

class bmpframe(wx.Frame): 
    def __init__(self, *args, **kwds): 
     wx.Frame.__init__(self, *args, **kwds) 

     pnl = wx.Panel(self, -1) 
     # lazy way to make two discernable bitmaps 
     # Warning: alpha does not work on every platform/version 
     bmp1 = wx.EmptyBitmapRGBA(64, 64, alpha=0) 
     bmp2 = wx.EmptyBitmapRGBA(64, 64, alpha=1) 

     static_bitmap_1 = wx.StaticBitmap(pnl, -1, bitmap=bmp1) 
     static_bitmap_2 = wx.StaticBitmap(pnl, -1, bitmap=bmp2) 
     self.stbmp1 = static_bitmap_1 
     self.stbmp2 = static_bitmap_2 

     self.btn_swap = wx.Button(pnl, -1, u'Swap…') 

     szmain = wx.BoxSizer(wx.VERTICAL) 
     szmain.Add(static_bitmap_1, 0, wx.EXPAND|wx.ALL, 4) 
     szmain.Add(static_bitmap_2, 0, wx.EXPAND|wx.ALL, 4) 
     szmain.Add(self.btn_swap, 0, wx.EXPAND|wx.ALL, 4) 

     pnl.SetSizer(szmain) 
     szmain.Fit(self) 

     self.btn_swap.Bind(wx.EVT_BUTTON, self.on_swap) 

    def on_swap(self, evt): 
     print 'EVT_BUTTON' 
     bmp1 = self.stbmp1.GetBitmap() 
     bmp2 = self.stbmp2.GetBitmap() 
     self.stbmp1.SetBitmap(bmp2) 
     self.stbmp2.SetBitmap(bmp1) 
     self.Refresh() 

if __name__ == '__main__': 
    app = wx.App(redirect=False) 
    frm = bmpframe(None, -1, 'testbmpswap') 
    frm.Show() 

    app.MainLoop() 
+0

是的,ic是一個StaticBitmap的列表。 'self.ic.append(wx.StaticBitmap(self,wx.ID_ANY,wx.BitmapFromImage(img1),name =「Pic1」))' 非常感謝。你的代碼解決了這個問題。 – Joydeep 2015-02-24 17:10:59