Trever Wagenhals
Trever Wagenhals

Reputation: 395

Make one function handle multiple button clicks in wxpython

Good evening/morning,

I have been posting about this related subject a bit and you may even find a post of mine that is similar to this, although I am having trouble conceptually applying the answer to another problem to this circumstance.

I have a program that repeats the same layout 4 times in the window. Because of this, I have found that I am declaring every button 4 times, which is annoying. So, I posted and was able to fix this issue. The issue now is that I do not know how to do this for when a button click opens a new window.

Here is the code:

def make_button(text, callback, starty, startx, height, width):
    button = wx.Button(self, -1, text)
    sizer.Add(button, (starty, startx), (height, width), wx.EXPAND)
    button.Bind(wx.EVT_BUTTON, callback)
    return button

self.Rail1ConfigSlot1 = make_button("Configure", self.Rail1ConfigSlot1_clicked, 10, 1, 1, 1)
self.Rail2ConfigSlot1 = make_button("Configure", self.Rail2ConfigSlot1_clicked, 10, 2, 1, 1)
self.Rail3ConfigSlot1 = make_button("Configure", self.Rail3ConfigSlot1_clicked, 10, 3, 1, 1)
self.Rail1ConfigSlot2 = make_button("Configure", self.Rail1ConfigSlot2_clicked, 10, 6, 1, 1)
self.Rail2ConfigSlot2 = make_button("Configure", self.Rail2ConfigSlot2_clicked, 10, 7, 1, 1)
self.Rail3ConfigSlot2 = make_button("Configure", self.Rail3ConfigSlot2_clicked, 10, 8, 1, 1)
self.Rail1ConfigSlot3 = make_button("Configure", self.Rail1ConfigSlot3_clicked, 10, 11, 1, 1)
self.Rail2ConfigSlot3 = make_button("Configure", self.Rail2ConfigSlot3_clicked, 10, 12, 1, 1)
self.Rail3ConfigSlot3 = make_button("Configure", self.Rail3ConfigSlot3_clicked, 10, 13, 1, 1)
self.Rail1ConfigSlot4 = make_button("Configure", self.Rail1ConfigSlot4_clicked, 10, 16, 1, 1)
self.Rail2ConfigSlot4 = make_button("Configure", self.Rail2ConfigSlot4_clicked, 10, 17, 1, 1)
self.Rail3ConfigSlot4 = make_button("Configure", self.Rail3ConfigSlot4_clicked, 10, 18, 1, 1)


def Rail1ConfigSlot1_clicked(self, event):
self.Rail1ConfigSlot1_window = NewWindow(parent=None, id= -1)
self.Rail1ConfigSlot1_window.Show()

def Rail2ConfigSlot1_clicked(self, event):
self.Rail2ConfigSlot1_window = NewWindow(parent=None, id= -1)
self.Rail2ConfigSlot1_window.Show()

def Rail3ConfigSlot1_clicked(self, event):
self.Rail3ConfigSlot1_window = NewWindow(parent=None, id= -1)
self.Rail3ConfigSlot1_window.Show()

def Rail1ConfigSlot2_clicked(self, event):
self.Rail1ConfigSlot2_window = NewWindow(parent=None, id= -1)
self.Rail1ConfigSlot2_window.Show()

def Rail2ConfigSlot2_clicked(self, event):
self.Rail2ConfigSlot2_window = NewWindow(parent=None, id= -1)
self.Rail2ConfigSlot2_window.Show()

def Rail3ConfigSlot2_clicked(self, event):
self.Rail3ConfigSlot2_window = NewWindow(parent=None, id= -1)
self.Rail3ConfigSlot2_window.Show()

def Rail1ConfigSlot3_clicked(self, event):
self.Rail1ConfigSlot3_window = NewWindow(parent=None, id= -1)
self.Rail1ConfigSlot3_window.Show()

def Rail2ConfigSlot3_clicked(self, event):
self.Rail2ConfigSlot3_window = NewWindow(parent=None, id= -1)
self.Rail2ConfigSlot3_window.Show()

def Rail3ConfigSlot3_clicked(self, event):
self.Rail3ConfigSlot3_window = NewWindow(parent=None, id= -1)
self.Rail3ConfigSlot3_window.Show()

def Rail1ConfigSlot4_clicked(self, event):
self.Rail1ConfigSlot4_window = NewWindow(parent=None, id= -1)
self.Rail1ConfigSlot4_window.Show()

def Rail2ConfigSlot4_clicked(self, event):
self.Rail2ConfigSlot4_window = NewWindow(parent=None, id= -1)
self.Rail2ConfigSlot4_window.Show()

def Rail3ConfigSlot4_clicked(self, event):
self.Rail3ConfigSlot4_window = NewWindow(parent=None, id= -1)
self.Rail3ConfigSlot4_window.Show()

class NewWindow(wx.Frame):
    def __init__(self,parent,id):
       wx.Frame.__init__(self, parent, id, 'New Window', size=(400,300))
       wx.Frame.CenterOnScreen(self)
       #self.new.Show(False)

So, how can I make a definition that is universal to handle all of these 12 almost identical buttons, yet have the windows be individual and independent to each other?

Upvotes: 0

Views: 271

Answers (2)

Trever Wagenhals
Trever Wagenhals

Reputation: 395

My solution was derived from @Mike Driscoll, who gave me the idea for the for loop. The only problem with the for loop he created was that I accidentally had the wrong locations entered, and so his values didn't actually match the button positions of my code. This is the way I did it to get the buttons positioned correctly.

for num in range(1, 4):
    make_button("Configure", self.RailConfig_clicked, 10, num, 1 ,1)
    make_button("Configure", self.RailConfig_clicked, 10, num+5, 1, 1)
    make_button("Configure", self.RailConfig_clicked, 10, num+10, 1, 1) 
    make_button("Configure", self.RailConfig_clicked, 10, num+15, 1, 1) 

def RailConfig_clicked(self, event):
    self.Rail1ConfigSlot1_window = NewWindow(parent=None, id= -1)
    self.Rail1ConfigSlot1_window.Show()

Upvotes: 0

Mike Driscoll
Mike Driscoll

Reputation: 33111

I don't understand why you need different event handlers for each of these buttons. They all open a new instance of NewWindow after all. If you bind all the buttons to the same event handler, then you can use a loop to create the buttons:

for num in range(1, 13):
    make_button("Configure", self.onConfigure, num, 9, 1, 1)

def onConfigure(self, event):
    new_window = NewWindow(parent=None, id=-1)
    new_window.Show()

Upvotes: 1

Related Questions