2012-06-22 52 views
0

如何向wxListBox項目添加額外數據。我正在創建一個照片查看應用程序,用戶雙擊圖像的文件路徑以打開圖像。當用戶雙擊單擊列表框中的項目時,函數會執行listbox1.GetStringSelection()以使用cuurent選定文件當打開一個StaticBitmap圖像。但是,這顯示整個文件路徑看起來很醜,所以如何才能改變它只顯示文件名?我怎樣才能爲每個列表框項目添加額外的數據?如何向wxListBox項目添加額外數據

萊\ Windows邊欄\小工具\ Weather.Gadget \ \圖像120DPI(120DPI)alertIcon.png

C:\ Program Files文件\ Windows邊欄\小工具\ Weather.Gadget \ \圖像120DPI(120DPI)grayStateIcon巴紐

C:\ Program Files文件\ Windows邊欄\小工具\ Weather.Gadget \ \圖像120DPI(120DPI)greenStateIcon.png

C:\ Program Files文件\ Windows邊欄\小工具\ Weather.Gadget \圖片\ 120DPI(120DPI)notConnectedStateIcon.png

C:\ Program Files \ W indows Sidebar \ Gadgets \ Weather.Gadget \ images \ 144DPI(144DPI)alertIcon.png

+0

http://stackoverflow.com/questions/4433715/how-can-i-store-objects-other-than-strings-in-a-wxpython-的欺騙組合框雖然它是一個組合框,但它們都從同一個父類繼承此功能 – GP89

回答

3

我在前面寫過關於這個的blog。你應該檢查出來。基本上你只需將列表中的每個項目追加到列表框中,並添加一些額外的數據作爲第二個參數。在我的例子中,我添加了一個對象實例。下面是從我的博客文章代碼:

import wx 

######################################################################## 
class Car: 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, id, model, make, year): 
     """Constructor""" 
     self.id = id 
     self.model = model 
     self.make = make 
     self.year = year  


######################################################################## 
class MyForm(wx.Frame): 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial") 

     # Add a panel so it looks the correct on all platforms 
     panel = wx.Panel(self, wx.ID_ANY) 

     ford = Car(0, "Ford", "F-150", "2008") 
     chevy = Car(1, "Chevrolet", "Camaro", "2010") 
     nissan = Car(2, "Nissan", "370Z", "2005") 

     sampleList = [] 
     lb = wx.ListBox(panel, 
         size=wx.DefaultSize, 
         choices=sampleList) 
     self.lb = lb 
     lb.Append(ford.make, ford) 
     lb.Append(chevy.make, chevy) 
     lb.Append(nissan.make, nissan) 
     lb.Bind(wx.EVT_LISTBOX, self.onSelect) 

     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(lb, 0, wx.ALL, 5) 
     panel.SetSizer(sizer) 

    #---------------------------------------------------------------------- 
    def onSelect(self, event): 
     """""" 
     print "You selected: " + self.lb.GetStringSelection() 
     obj = self.lb.GetClientData(self.lb.GetSelection()) 
     text = """ 
     The object's attributes are: 
     %s %s %s %s 

     """ % (obj.id, obj.make, obj.model, obj.year) 
     print text 

# Run the program 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm() 
    frame.Show() 
    app.MainLoop() 
相關問題