0

GUIアプリケーションをwxpythonに実装しました。メインウィンドウには、ファイルの名前を表示するために使用されるlistctrlがあります。最初は空でした。ユーザーが[ファイル]、[開く]の順にクリックし、開くファイルを選択します。これを[OK]ボタンをクリックして行うと、ファイルの名前が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"動作し、次の2つのprint句も動作します。しかし、メインウィンドウのlistctrlは変更されませんでした。つまり、 level 1andlevel 2が表示されず、listctrlの背景が赤に変更されませんでした。理由は何ですか?どうもありがとう!

4

1 に答える 1

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()
于 2010-08-26T21:41:54.000 に答える