tkinter creating buttons in for loop passing command arguments

前端 未结 3 1325
陌清茗
陌清茗 2020-11-22 01:09

I am trying to create buttons in tkinter within a for loop. And with each loop pass the i count value out as an argument in the command value. So when the function is called

3条回答
  •  不思量自难忘°
    2020-11-22 01:52

    This is how closures work in python. I ran into this problem myself once. You could use functools.partial for this.

    for i in range(3):
        self.button.append(Button(self, text='Game '+str(i+1), command=partial(self.open_this, i)))
    

提交回复
热议问题