0

以下は、名前のない statictext を作成する私のコードです。
このコードでラベルを変更する方法はありますか?

import wx


class Mainframe(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        self.panel = wx.Panel(self)
        wx.StaticText(self.panel, id=1, label='test', pos=(10, 30))
        wx.StaticText(self.panel, id=2, label='test', pos=(10, 60))


if __name__ == '__main__':
        app = wx.App(False)
        frame = Mainframe(None)
        frame.Show()
        app.MainLoop()
4

1 に答える 1

0

まず、独自の ID を指定しないでください。特に 1000 未満の ID は、wxPython で内部的に予約されている可能性があるためです。ID を指定する場合は、次のようにします。

myId1 = wx.NewId()
myId2 = wx.NewId()
wx.StaticText(self.panel, id=myId1, label='test', pos=(10, 30))
wx.StaticText(self.panel, id=myId2, label='test', pos=(10, 60))

ウィジェットを調べたい場合は、wx.FindWindowById や wx.FindWindowByName などの wxPython FindWindowBy* メソッドのいずれかを試すことをお勧めします。

簡単な例を次に示します。

import wx    

class Mainframe(wx.Frame):
    myId1 = wx.NewId()
    myId2 = wx.NewId()

    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        self.panel = wx.Panel(self)

        sizer = wx.BoxSizer(wx.VERTICAL)

        txt1 = wx.StaticText(self.panel, id=self.myId1, 
                             label='test', name="test1")
        txt2 = wx.StaticText(self.panel, id=self.myId2, 
                             label='test', name="test2")

        btn = wx.Button(self.panel, label="Find by id")
        btn.Bind(wx.EVT_BUTTON, self.onFindById)
        btn2 = wx.Button(self.panel, label="Find by name")
        btn2.Bind(wx.EVT_BUTTON, self.onFindByName)

        sizer.Add(txt1, 0, wx.ALL, 5)
        sizer.Add(txt2, 0, wx.ALL, 5)
        sizer.Add(btn, 0, wx.ALL, 5)
        sizer.Add(btn2, 0, wx.ALL, 5)

        self.panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onFindById(self, event):
        """"""
        print "myId1 = " + str(self.myId1)
        print "myId2 = " + str(self.myId2)

        txt1 = wx.FindWindowById(self.myId1)
        print type(txt1)

        txt2 = wx.FindWindowById(self.myId2)
        print type(txt2)

    #----------------------------------------------------------------------
    def onFindByName(self, event):
        """"""
        txt1 = wx.FindWindowByName("test1")
        txt2 = wx.FindWindowByName("test2")

if __name__ == '__main__':
        app = wx.App(False)
        frame = Mainframe(None)
        frame.Show()
        app.MainLoop()
于 2013-09-20T13:35:17.103 に答える