我正在学习wx,我遇到了这样的问题:假设我创建了几个StaticText对象,然后我想在某些事件上更改其标签。 例如:
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent=parent)
self.panel=wx.Panel(self)
for i in range(5):
self.button=wx.Button(self.panel, -1, label='b'+str(i), pos=(20,30*i))
self.button.Bind(wx.EVT_BUTTON, self.on_button)
self.label=wx.StaticText(self.panel, -1, label='label'+str(i), pos=(120,30*i), name='label'+str(i))
def on_button(self, event):
b=event.GetEventObject().GetLabel()
if b.endswith('1'):
self.label1.SetLabel('sss')
x=wx.App()
y=MyFrame(None).Show()
x.MainLoop()
所以,正如你所看到的,我想改变我之前创建的self.label1的标签,因为一个错误我不能这样做:在on_button; AttributeError:'MyFrame'对象没有属性'label1'
我认为在创建对象时我必须改变一些东西,但我不知道是什么。
答案 0 :(得分:0)
您可以使用dict将按钮映射到标签:
self.label[button] = label
然后,给定与按钮关联的事件,您可以找到关联的标签:
button = event.GetEventObject()
label = self.label[button]
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent=parent)
self.panel = wx.Panel(self)
self.label = {}
for i in range(5):
button = wx.Button(
self.panel, -1, label='b' + str(i), pos=(20, 30 * i))
button.Bind(wx.EVT_BUTTON, self.on_button)
label = wx.StaticText(self.panel, -1, label='label' + str(
i), pos=(120, 30 * i), name='label' + str(i))
self.label[button] = label
def on_button(self, event):
button = event.GetEventObject()
label = self.label[button]
label.SetLabel('sss')
x = wx.App()
y = MyFrame(None).Show()
x.MainLoop()
答案 1 :(得分:0)
第一个问题是,在每次迭代通过for循环时,都会覆盖self.button和self.label实例。你永远不会创建一个名为“self.label1”的变量。有几种方法可以解决这个问题。这是一种简单的方法:
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent=parent)
self.panel=wx.Panel(self)
for i in range(5):
self.button=wx.Button(self.panel, -1, label='b'+str(i), pos=(20,30*i))
self.button.Bind(wx.EVT_BUTTON, self.on_button)
self.label=wx.StaticText(self.panel, -1, label='label'+str(i), pos=(120,30*i), name='label'+str(i))
def on_button(self, event):
b=event.GetEventObject().GetLabel()
if b.endswith('1'):
newLabel = "sss"
self.resetLabel('1', newLabel)
elif b.endswith('2'):
self.resetLabel('2', "Number 2!")
#----------------------------------------------------------------------
def resetLabel(self, number, newLabel):
""""""
lbls = [widget for widget in self.panel.GetChildren() if isinstance(widget, wx.StaticText)]
for lbl in lbls:
if number in lbl.GetLabel():
lbl.SetLabel(newLabel)
break
x=wx.App()
y=MyFrame(None).Show()
x.MainLoop()
你也可以使用setattr和getattr动态创建self.label(1-N):
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent=parent)
self.panel=wx.Panel(self)
for i in range(5):
setattr(self, "button%s" % i,
wx.Button(self.panel, -1, label='b'+str(i), pos=(20,30*i))
)
btn = getattr(self, "button%s" % i)
btn.Bind(wx.EVT_BUTTON, self.on_button)
setattr(self, "label%s" % i,
wx.StaticText(self.panel, -1, label='label'+str(i), pos=(120,30*i), name='label'+str(i))
)
print
def on_button(self, event):
b=event.GetEventObject().GetLabel()
if b.endswith('1'):
self.label1.SetLabel("sss")
elif b.endswith('2'):
self.label2.SetLabel("Number 2!")
x=wx.App()
y=MyFrame(None).Show()
x.MainLoop()