我创建了一个巨大的代码,我的程序有点慢,我想问一下有没有办法将多个小部件绑定到同一个处理程序中......请参阅下面的代码的某些部分
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')
代码的最后两部分分别重复了四次。
是的,您可以将多个小部件绑定到同一个处理程序。您只需要为每个小部件分配一个名称或一个唯一的 ID。我推荐这个名字,因为你不想意外地将系统 ID 用于你的一个小部件之一。无论如何,我写了一篇文章在这里解释了这一切:
- http://www.blog.pythonlibrary.org/2011/09/20/wxpython-binding-multiple-widgets-to-the-same-handler/
主要思想是,在处理程序中,您可以从事件中获取小部件。GetEventObject().然后,您可以使用适当的调用来获取其名称,ID,标签等:widget。GetId(), widget.GetLabel(), widget.获取名称(),等等。