我创建了一个巨大的代码,我的程序有点慢,我想问一下是否有办法将多个小部件绑定到同一个处理程序中......请参阅下面我的代码的某些部分
self.button=AB.AquaButton(self,label="Sensor 1",pos=(10,10),size=(90,35))
self.Bind(wx.EVT_BUTTON, self.OnButton, self.button)
self.button.SetForegroundColour(wx.Colour(0,0,0))
self.button1=AB.AquaButton(self,label="Sensor 2",pos=(110,10),size=(90,35))
self.Bind(wx.EVT_BUTTON, self.OnButton1, self.button1)
self.button1.SetForegroundColour(wx.Colour(0,0,0))
self.button2=AB.AquaButton(self,label="Sensor 3",pos=(10,50),size=(90,35))
self.Bind(wx.EVT_BUTTON, self.OnButton2, self.button2)
self.button2.SetForegroundColour(wx.Colour(0,0,0))
self.button3=AB.AquaButton(self,label="Sensor 4",pos=(110,50),size=(90,35))
self.Bind(wx.EVT_BUTTON, self.OnButton3, self.button3)
self.button3.SetForegroundColour(wx.Colour(0,0,0))
self.button4=AB.AquaButton(self,label="Sensor 5",pos=(10,90),size=(90,35))
self.Bind(wx.EVT_BUTTON, self.OnButton4, self.button4)
self.button4.SetForegroundColour(wx.Colour(0,0,0))
self.button5=AB.AquaButton(self,label="OK",pos=(110,142),size=(90,35))
self.Bind(wx.EVT_BUTTON, self.OnButton5, self.button5)
self.button5.SetForegroundColour(wx.Colour(0,0,0))
def OnButton(self, event):
self.new = OtherFrame0()
self.new.Show()
def OnButton1(self, event):
self.new = OtherFrame1()
self.new.Show()
def OnButton2(self, event):
self.new = OtherFrame2()
self.new.Show()
def OnButton3(self, event):
self.new = OtherFrame3()
self.new.Show()
def OnButton4(self, event):
self.new = OtherFrame4()
self.new.Show()
def OnButton5(self, event):
self.Close()
class OtherFrame0(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Edit Name", size=(210,80),
style=wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)
panel = wx.Panel(self)
self.CenterOnParent()
self.SetBackgroundColour('#e4e4e4')
self.msgTxt = wx.TextCtrl(self, -1,('Sensor 1'),pos=(10,10))
self.button=AB.AquaButton(self,label="Set",pos=(120,10),size=(85,30))
self.Bind(wx.EVT_BUTTON, self.onSendAndClose, self.button)
self.button.SetForegroundColour(wx.Colour(0,0,0))
def onSendAndClose(self, event):
msg0 = self.msgTxt.GetValue()
Publisher().sendMessage(("show.mainframe0"), msg0)
sheet1.write(0, 0,msg0)
book.save('Application.xls')
self.Close()
msg = "Sensor 1"
instructions = wx.StaticText(self,label=msg,pos=(15,53))
self.pubsubText0 = wx.TextCtrl(self, value="Sensor 1",
pos(75,50),style=wx.TE_READONLY)
self.textctrl = wx.TextCtrl(self,value="", pos=(180, 50),
style=wx.TE_READONLY)
self.pubsubText0.SetBackgroundColour('#e4e4e4')
代码的最后两部分各重复了四次。
答案 0 :(得分:3)
是的,您可以将多个小部件绑定到同一个处理程序。您只需要为每个小部件指定一个名称或一个唯一的ID。我建议使用该名称,因为您不希望意外地将系统ID用于其中一个小部件。无论如何,我写了一篇文章解释了这一切:
主要思想是在处理程序中,您可以从event.GetEventObject()获取窗口小部件。然后你可以使用适当的调用来获取它的名字,id,标签等:widget.GetId(),widget.GetLabel(),widget.GetName()等。