button1 = Button(master, text='Search')button1.configure(command=lambda widget=button1: DoSomething(widget))button2 = Button(master, text='Search')button2.configure(command=lambda widget=button2: DoSomething(widget))
另一个选择是,如果您确实不需要引用小部件,则只需传递文字字符串:
button1 = Button(..., command=lambda widget="button1": DoSomething(widget))button2 = Button(..., command=lambda widget="button2": DoSomething(widget))
另一个选择是给每个按钮一个唯一的回调,并使该回调仅执行该按钮唯一的 *** 作:
button1 = Button(..., command=ButtonOneCallback)button2 = Button(..., command=ButtonTwoCallback)def ButtonOneCallback(): value = user_input.get() DoSomething(value)def ButtonTwoCallback(): value=choice.get(choice.curselection()[0]) DoSomething(value)def DoSomething(value): ...
还有其他方法可以解决相同的问题,但是希望这可以使您大致了解如何将值传递给按钮回调,或者首先可以避免这样做。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)