0

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?

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

3 Comments

I apologize but the positioning of the buttons were actually labeled wrong. I edited it, so now your loop doesn't actually work. That is why I was more interested in a function to do it and pass different parameters into the new window that is created at some point
I supposed I could still use nested for loops to do it the way you presented then though
You're already passing the position of the buttons to the make_button function, so I'm not sure why this code wouldn't work.
0

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()

1 Comment

For some reason, this doesn't allow me to pass parameters to Rail1Config_clicked using the "lamda event: self.Rail1Config_clicked(event, parameter) method. This poses a huge problem as now all the buttons are linked to the same new window, but I am unable to pass parameters to vary the windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.