2010-08-26 70 views
0

我在wxpython中實現了一個GUI應用程序,在主窗口上有一個listctrl用於顯示文件的名稱。它在一開始就是空的。用戶點擊「文件」,然後「打開」,然後選擇一個文件打開,當點擊「確定」按鈕完成後,該文件的名稱應該顯示在listctrl中。但似乎這是行不通的。我用print子句檢查,print子句起作用。這裏是我的代碼: 關於wxpython listctrl的問題

def OnDisplay(self): 
    print "On display called" 
    self.lc1.InsertStringItem(0, "level 1") 
    self.lc1.InsertStringItem(1, "level 2") 
    self.lc1.SetBackgroundColour(wx.RED) 

    print self.lc1.GetItemText(0) 
    print self.lc1.GetItemText(1) 

    self.lc1.Refresh() 

lc1是的listctrl,它是在何時被lauched主窗口一開始初始化,但是當OnDisplay被觸發,print "On display called"作品,並將下列兩項print條款也行。但主窗口上的listctrl並沒有改變,我的意思是,沒有顯示level 1level 2,listctrl的背景也沒有改爲紅色,請問請問是什麼原因?非常感謝!

+0

做工精細的蟒蛇2.6,wxPython的2.8,Windows 7的 – volting 2010-08-26 21:16:21

+0

@volting:好,我使用python2.6的和Windows Vista中... – serina 2010-08-26 21:30:55

+0

應該不會出現Vista和7也許有什麼區別你的代碼中的其他東西正在影響... ...發佈一個可運行的示例,你可以看到這是否適用於你 – volting 2010-08-26 21:39:02

回答

0

這是一個可運行的例子,適用於Windows 7,Python 2.6,wx 2.8。

import wx 

class ListTest(wx.Frame): 
    def __init__(self, parent, title): 
     wx.Frame.__init__(self, parent, -1, title, size=(380, 230)) 

     panel = wx.Panel(self, -1) 

     self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT) 
     self.list.InsertColumn(0, 'col 1', width=140) 

     hbox = wx.BoxSizer(wx.HORIZONTAL) 
     hbox.Add(self.list, 1, wx.EXPAND) 
     panel.SetSizer(hbox) 
     self.Centre() 
     self.Show(True) 

     self.Bind(wx.EVT_CHAR_HOOK, self.onKey) 

    def onKey(self, evt): 
     if evt.GetKeyCode() == wx.WXK_DOWN: 
      self.list.InsertStringItem(0, "level 1") 
      self.list.InsertStringItem(1, "level 2") 
      self.list.SetBackgroundColour(wx.RED) 
      self.list.Refresh() 

      print self.list.GetItemText(0) 
      print self.list.GetItemText(1) 
     else: 
      evt.Skip() 


app = wx.App() 
ListTest(None, 'list test') 
app.MainLoop()