关于wxpython listctrl的一个问题

发布于 2024-09-15 20:26:41 字数 814 浏览 10 评论 0原文

我得到了一个用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的背景也没有变成红色,请问是什么原因?非常感谢!

I got a GUI application implemented in wxpython, on the main window, there is a listctrl used to dispaly the names of the files. it was empty at the very beginning. the user clicks the "File", then "open", then chooses a file to open, when this is done by clicking on the "ok" button, the names of the file is supposed to be display in the listctrl. But it seems that this does not work. I used a print clause to check, the print clause works. Here are my codes:

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 is the listctrl, it was initialized at the very beginning when the main window was lauched, but when the OnDisplay was triggered, the print "On display called" works, and the following two print clauses also work. but the listctrl on the main window did not change, i mean, did not show the level 1 and level 2, nor did the background of the listctrl was changed to red, what is the reason please? many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光是把杀猪刀 2024-09-22 20:26:41

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

Here is a runnable example that works on 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文