0

here is python program that produces a list of drives and adds buttons accordingly,

    drives = win32api.GetLogicalDriveStrings()
    drives = (drives.split('\000')[:-1])
    for d in range(0,len(drives)):
        box.add_widget(Button(text=drives[d],on_press = lambda x: self.open_drive(s=drives[0+d])))

    self.add_widget(box)

def open_drive(self,s):
    print(str(s))

when ever I click the button it is supposed to print C: D: E: and so on, but it is stuck on last drive in the list, how do i store the parameter in that lambda function?

shashikant_
  • 122
  • 5

1 Answers1

0

My head hurts, but you could use:

for d in range(0,len(drives)):
        box.add_widget(Button(text=drives[d],
                              on_press = (lambda d: lambda x: self.open_drive(s=drives[0+d]))(d))

This way you build a lambda function that returns another lambda function, only this last one uses a known value passed as a parameter by that first lambda which is called each cicle of the loop :)

mikelsr
  • 457
  • 6
  • 13