晚上好/早上好,
我一直在发布有关这个相关主题的内容,你甚至可能找到一个类似于此的帖子,虽然我在概念上将问题的答案应用于另一个问题。
我有一个程序在窗口中重复相同的布局4次。因此,我发现我将每个按钮声明4次,这很烦人。所以,我发布并能够解决这个问题。现在的问题是,当按钮单击打开一个新窗口时,我不知道如何执行此操作。
以下是代码:
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)
那么,我如何制作一个通用的定义来处理所有这12个几乎相同的按钮,但是窗口是个体的并且彼此独立?
答案 0 :(得分:1)
我不明白为什么每个按钮需要不同的事件处理程序。毕竟,他们都打开了NewWindow
的新实例。如果将所有按钮绑定到同一个事件处理程序,则可以使用循环创建按钮:
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()
答案 1 :(得分:0)
我的解决方案来自@Mike Driscoll,他给了我for循环的想法。他创建的for循环的唯一问题是我不小心输入了错误的位置,因此他的值实际上与我的代码的按钮位置不匹配。这就是我按钮正确定位按钮的方式。
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()