我在带有tkinter的python界面中有4个按钮。 我需要从每个按钮调用一个不同的函数。 因此,我需要找出上次按下哪个按钮来调用特定功能。
答案 0 :(得分:0)
您可以为函数提供参数[特定于每个按钮],该函数将通过按钮按下事件来调用。在该函数中,您将必须将给定的参数与按钮ID进行比较,以找出被按下的参数。要按下上一个按钮,您必须将该ID存储在变量中。
pre_button_id = 0 ## variable to store previous button id
## for future iterations
def test_function(button_id):
global pre_button_id
pre_button_id=button_id
if button_id==1:
## call function 1 here
elif button_id==2:
## call function 2 here
elif button_id==3:
## call function 3 here
elif button_id==4:
## call function 4 here
B1 = Button(root, text="1",command=lambda:test_function(1))
B2 = Button(root, text="2",command=lambda:test_function(2))
B3 = Button(root, text="3",command=lambda:test_function(3))
B4 = Button(root, text="4",command=lambda:test_function(4))