【问题标题】:a problem about wxpython listctrl关于wxpython listctrl的一个问题
【发布时间】:2010-08-26 20:23:14
【问题描述】:

我有一个用 wxpython 实现的 GUI 应用程序,在主窗口上,有一个 listctrl 用于显示文件的名称。一开始它是空的。用户单击“文件”,然后单击“打开”,然后选择要打开的文件,当通过单击“确定”按钮完成此操作时,文件的名称应该显示在 listctrl 中。但这似乎行不通。我使用print 子句检查,print 子句有效。这是我的代码:

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,一开始在主窗口启动时就初始化了,但是当OnDisplay被触发时,print "On display called"起作用,下面两个print子句也起作用。但是主窗口的listctrl没有改变,我的意思是,没有显示level 1level 2,也没有将listctrl的背景变成红色,请问是什么原因?非常感谢!

【问题讨论】:

  • 在 python 2.6、wxpython 2.8、windows 7 上运行良好。
  • @volting:好吧,我用的是python2.6和windows vista...
  • vista 和 7 之间应该没有任何区别。也许你的代码中的其他东西正在影响......我会发布一个可运行的示例,你可以看看它是否适合你
  • @volting:谢谢,我会检查代码:)

标签: wxpython listctrl


【解决方案1】:

这是一个可在 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 2011-10-13
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多