【问题标题】:how to find out which button was pressed last time python tkinter如何找出上次按下哪个按钮 python tkinter
【发布时间】:2019-08-11 14:06:26
【问题描述】:

我在带有 tkinter 的 python 界面中有 4 个按钮。 我需要从每个按钮调用不同的函数。 因此,我需要找出上次按下哪个按钮来调用特定功能。

【问题讨论】:

  • 你是否给所有按钮都设置了相同的command回调?
  • 您是否尝试过简单地将命令保存在变量中,并在每次调用时更新变量?
  • 不,是不同的功能
  • 如果您已将command 选项设置为不同按钮的不同功能,那么您已经完成了您需要的操作。还是您的意思是您需要检查之前在这些不同的功能中按下了哪个按钮?
  • 我解决了,非常感谢!

标签: python user-interface button tkinter interface


【解决方案1】:

您可以为将由按钮按下事件调用的函数提供一个参数[特定于每个按钮]。在该函数中,您必须将给定的参数与按钮 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多