在wxpython中使用for循环创建多个按钮会导致问题

时间:2016-04-11 17:00:31

标签: loops user-interface for-loop lambda wxpython

晚上好/早上好,

我在wxpython中编写一个程序,重复大部分代码4次,所以我一直试图缩短它。当我尝试使用for循环缩短它时,这是一个不起作用的部分:

# Handles the creation of all of the buttons
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

make_button("Margin Low", lambda event: self.MarginLow(event, 1), 1, 2, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 2), 1, 7, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 3), 1, 12, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 4), 1, 17, 1, 1)

make_button("Margin High", lambda event: self.MarginHigh(event, 1), 1, 3, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 2), 1, 8, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 3), 1, 13, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 4), 1, 18, 1, 1)

make_button("Margin Off", lambda event: self.MarginOff(event, 1), 1, 4, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 2), 1, 9, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 3), 1, 14, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 4), 1, 19, 1, 1)

make_button("Margin Low", lambda event: self.MarginLow(event, 1), 12, 1, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 2), 12, 6, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 3), 12, 11, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 4), 12, 16, 1, 1)

make_button("Margin High", lambda event: self.MarginHigh(event, 1), 11, 1, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 2), 11, 6, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 3), 11, 11, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 4), 11, 16, 1, 1)

make_button("Margin Off", lambda event: self.MarginOff(event, 1), 13, 1, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 2), 13, 6, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 3), 13, 11, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 4), 13, 16, 1, 1)

make_button("Margin Low", lambda event: self.MarginLow(event, 1), 12, 2, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 2), 12, 7, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 3), 12, 12, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 4), 12, 17, 1, 1)

make_button("Margin High", lambda event: self.MarginHigh(event, 1), 11, 2, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 2), 11, 7, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 3), 11, 12, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 4), 11, 17, 1, 1)

make_button("Margin Off", lambda event: self.MarginOff(event, 1), 13, 2, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 2), 13, 7, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 3), 13, 12, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 4), 13, 17, 1, 1)

make_button("Margin Low", lambda event: self.MarginLow(event, 1), 12, 3, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 2), 12, 8, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 3), 12, 13, 1, 1)
make_button("Margin Low", lambda event: self.MarginLow(event, 4), 12, 18, 1, 1)

make_button("Margin High", lambda event: self.MarginHigh(event, 1), 11, 3, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 2), 11, 8, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 3), 11, 13, 1, 1)
make_button("Margin High", lambda event: self.MarginHigh(event, 4), 11, 18, 1, 1)

make_button("Margin Off", lambda event: self.MarginOff(event, 1), 13, 3, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 2), 13, 8, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 3), 13, 13, 1, 1)
make_button("Margin Off", lambda event: self.MarginOff(event, 4), 13, 18, 1, 1)

def MarginLow(self, event, slot):
    print slot
def MarginHigh(self, event, slot):
    print slot
def MarginOff(self, event, slot):
    print slot

我能够将此代码转换为这样的for循环:

# Create all of the margining buttons for each slot
for i in range(0, 4):
    # Global Rail commands for all 4 slots
    make_button("Margin Low", lambda event: self.MarginLow(event, i), 1, (i*5)+2, 1, 1)
    make_button("Margin High", lambda event: self.MarginHigh(event, i), 1, (i*5)+3, 1, 1)
    make_button("Margin Off", lambda event: self.MarginOff(event, i), 1, (i*5)+4, 1, 1)
    # Rail 1 for all 4 slots
    make_button("Margin High", lambda event: self.MarginHigh(event, i), 11, (i*5)+1, 1, 1)
    make_button("Margin Low", lambda event: self.MarginLow(event, i), 12, (i*5)+1, 1, 1)
    make_button("Margin Off", lambda event: self.MarginOff(event, i), 13, (i*5)+1, 1, 1)
    # Rail 2 for all 4 slots
    make_button("Margin High", lambda event: self.MarginHigh(event, i), 11, (i*5)+2, 1, 1)
    make_button("Margin Low", lambda event: self.MarginLow(event, i), 12, (i*5)+2, 1, 1)
    make_button("Margin Off", lambda event: self.MarginOff(event, i), 13, (i*5)+2, 1, 1)
    # Rail 3 for all 4 slots
    make_button("Margin High", lambda event: self.MarginHigh(event, i), 11, (i*5)+3, 1, 1)  
    make_button("Margin Low", lambda event: self.MarginLow(event, i), 12, (i*5)+3, 1, 1)
    make_button("Margin Off", lambda event: self.MarginOff(event, i), 13, (i*5)+3, 1, 1)

for循环的问题在于,由于某种原因,当我单击按钮并进入定义并执行“print slot”命令时,它总是打印出my循环的最高值。例如,我的for循环从0到3,因此它总是打印出3,即使我单击在第一个循环期间创建的按钮。如果我将其更高或更低,则调整以匹配最后一个数字。

那么,为什么传递位置参数很好但不是每个循环应该改变的lambda事件值?

0 个答案:

没有答案