我正在使用带有while循环的静态文本创建表,之后我想设置标签。我遇到了问题,因为它只适用于最后一个。这是我的代码:
import wx
class Mainframe(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.panel = wx.Panel(self)
def test(self,n):
while n <=5:
a = wx.StaticText(self.panel, label='bad', id=n, pos=(20,30*n))
n = n+1
return a
test(self,0)
if test(self,0).GetId()==1:
test(self,0).SetLabel('good')
if test(self,0).GetId()==5:
test(self,0).SetLabel('exelent')
if __name__=='__main__':
app = wx.App(False)
frame = Mainframe(None)
frame.Show()
app.MainLoop()
答案 0 :(得分:0)
当你返回时,它只是最后一个创建的控件,因为每次循环它都会被覆盖。 将控件附加到列表,然后您可以全部访问它们。 另请注意,您正在调用5次测试,因此您将拥有5组相互重叠的statictexts。
import wx
class Mainframe(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.panel = wx.Panel(self)
ctrls = []
for n in range(6):
ctrls.append(wx.StaticText(self.panel, label='bad',
pos=(20, 30 * n)))
ctrls[1].SetLabel('good')
ctrls[5].SetLabel('excellent')
if __name__ == '__main__':
app = wx.App(False)
frame = Mainframe(None)
frame.Show()
app.MainLoop()