Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何知道在tkinter中是否单击了按钮?_Python_Tkinter_Button_Click - Fatal编程技术网

Python 如何知道在tkinter中是否单击了按钮?

Python 如何知道在tkinter中是否单击了按钮?,python,tkinter,button,click,Python,Tkinter,Button,Click,如果单击按钮,我想执行一项任务。我该怎么做?我可以使用两个单独的函数来执行任务,但是在'hey'中的'if'块之后有一大块代码,我不想再次键入整个内容。我的代码是: from tkinter import * root = Tk() def hey(): if bt1 is clicked: #do something if bt2 is clicked: #do something #some piece of code bt1

如果单击按钮,我想执行一项任务。我该怎么做?我可以使用两个单独的函数来执行任务,但是在
'hey'
中的
'if'
块之后有一大块代码,我不想再次键入整个内容。我的代码是:

from tkinter import *

root = Tk()

def hey():
    if bt1 is clicked:
        #do something
    if bt2 is clicked:
        #do something

    #some piece of code

bt1 = Button(root, text = 'yes', command = hey)
bt2 = Button(root, text = 'no', command = hey)

bt1.pack()
bt2.pack()

根据您共享的内容,您可以使用参数在按钮中传递标志,例如

bt1 = Button(root, text = 'yes', command =lambda: hey(True))
bt2 = Button(root, text = 'no', command =lambda: hey(False))
在代码中,你可以

def hey(which): # `which` decides which statement to be followed
    if which:
        #do something
    else:
        #do something

根据您共享的内容,您可以使用参数在按钮中传递标志,例如

bt1 = Button(root, text = 'yes', command =lambda: hey(True))
bt2 = Button(root, text = 'no', command =lambda: hey(False))
在代码中,你可以

def hey(which): # `which` decides which statement to be followed
    if which:
        #do something
    else:
        #do something