Reputation: 19
I have created a huge code and my program is a bit slow,I want to ask if there is a way to bind multiple widgets into the same handler...see below some parts of my code
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')
The last two parts of the code repeated others four times each.
Upvotes: 1
Views: 1780
Reputation: 33071
Yes you can bind multiple widgets to the same handler. You just need to assign a name to each of the widgets or a unique id. I recommend the name because you don't want to accidentally use a system id to one of your widgets. Anyway, I wrote an article that explains it all here:
The main idea is that in the handler you can get the widget from event.GetEventObject(). Then you can grab its name, id, label, etc using the appropriate call: widget.GetId(), widget.GetLabel(), widget.GetName(), etc.
Upvotes: 3