I am not aware of any internal tkinter method to check if a button is pressed.

However you could connect the Button with a function that changes the value of a global variable, like in the following:

from Tkinter import *

master = Tk()

def callback():
    global buttonClicked
    buttonClicked = not buttonClicked 


buttonClicked  = False # Bfore first click

b = Button(master, text="Smth", command=callback)
b.pack()

mainloop()

The code, changes the variable value from False to True (or reverse) every time you press the button.

Answer from Charalamm on Stack Overflow
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ tk_button.htm
Tkinter Button
You can attach a function or a method to a button which is called automatically when you click the button. Here is the simple syntax to create this widget โˆ’ ... options โˆ’ Here is the list of most commonly used options for this widget.
Discussions

[Tkinter] Button command is executed as soon as the program is run without pressing the button
When you do command= onclick(), this calls onclick and passes its return value to Button. You need to pass the function itself without calling it, so no parentheses: btn1 = Button(main, text = 'Press to Begin', command= onclick) More on reddit.com
๐ŸŒ r/learnpython
5
19
December 30, 2021
python - Tkinter: Calling function when button is pressed - Stack Overflow
In addition to changing the commmand= keyword argument so it doesn't call the function when the tk.Buttons are created, I also removed the event argument from the corresponding function definitions since tkinter doesn't pass any arguments to widget command functions (and your function don't ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python tkinter detecting which button is pressed
Hey. I am making a tkinter application and I cannot figure out how to handle which button is pressed. I have a for loop which automatially creates a selected number of entry and button widgets: for x in range(0,len(self.Serial_order)):# CREATE A LIST OF ARRAYS BASED ON CURRENT CSV FILE AND... More on forum.allaboutcircuits.com
๐ŸŒ forum.allaboutcircuits.com
0
November 26, 2020
How to make button in Python Tkinter stay pressed until another one is pressed - Stack Overflow
I am trying to find a way Tkinter to make the Start button stay pressed until I press the Stop button. from Tkinter import * import tkMessageBox class MainWindow(Frame): def __init__(self): ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ tkinter tutorial โ€บ tkinter button
Tkinter Button - Python Tutorial
April 3, 2025 - Typically, an action is a function you want to execute when users click the button. To execute the function, you assign its name to the command parameter of the Button widget. This is called the command binding in Tkinter.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-check-which-button-was-clicked-in-tkinter
How to check which Button was clicked in Tkinter ? - GeeksforGeeks
July 29, 2024 - In this example, if the text 'It is an apple' is printed on the screen, we get to know 'Apple' button is pressed, else when 'It is a banana' is printed on the screen, we get to know 'Banana' button is pressed. ... # Import the library tkinter from tkinter import * # Create a GUI app app = Tk() # Create a function with one parameter to indicate which button was clicked def which_button(button_text): # Printing the text when a button is clicked print(f"Button clicked: {button_text}") # Creating and displaying of button b1 b1 = Button(app, text="Apple", command=lambda: which_button("Apple")) b1.grid(padx=10, pady=10) # Creating and displaying of button b2 b2 = Button(app, text="Banana", command=lambda: which_button("Banana")) b2.grid(padx=10, pady=10) # Make the infinite loop for displaying the app app.mainloop()
๐ŸŒ
Universidad de Granada
ccia.ugr.es โ€บ mgsilvente โ€บ tkinterbook โ€บ button.htm
The tkinter Button Widget
The Button widget is a standard tkinter widget used to implement various kinds of buttons. Buttons can contain text or images, and you can associate a Python function or method with each button. When the button is pressed, tkinter automatically calls that function or method.
๐ŸŒ
Python Course
python-course.eu โ€บ tkinter โ€บ buttons-in-tkinter.php
3. Buttons in Tkinter | Tkinter | python-course.eu
import tkinter as tk def write_slogan(): print("Tkinter is easy to use!") root = tk.Tk() frame = tk.Frame(root) frame.pack() button = tk.Button(frame, text="QUIT", fg="red", command=quit) button.pack(side=tk.LEFT) slogan = tk.Button(frame, text="Hello", command=write_slogan) slogan.pack(side=tk.LEFT) root.mainloop() The result of the previous example looks like this: The following script shows an example, where a label is dynamically incremented by 1 until a stop button is pressed:
Find elsewhere
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-tkinter-button-click-call-function
Call Function on Tkinter Button Click - Python Examples
def someFunction: function body tkWindow = Tk() button = Button(tkWindow) button['command'] = someFunction ยท In this example, we will create a function, a Tkinter Button, and assign the function to Button, such that when user clicks on the Button, the function is called.
Top answer
1 of 4
4

Here's a runnable answer. In addition to changing the commmand= keyword argument so it doesn't call the function when the tk.Buttons are created, I also removed the event argument from the corresponding function definitions since tkinter doesn't pass any arguments to widget command functions (and your function don't need it, anyway).

You seem to be confusing event handlers with widget command function handlers. The former do have an event argument passed to them when they're called, but the latter typically don't (unless you do additional stuff to make it happenโ€”it's a fairly common thing to need/want to do and is sometimes referred to as theThe extra arguments trick).

import tkinter as tk

# added only to define required global variable "txt"
class Txt(object):
    def SetValue(data): pass
    def GetValue(): pass
txt = Txt()
####

def load():
    with open(textField.get()) as file:
        txt.SetValue(file.read())

def save():
    with open(textField.get(), 'w') as file:
        file.write(txt.GetValue())

win = tk.Tk()
win.title('Text Editor')
win.geometry('500x500')

# create text field
textField = tk.Entry(win, width=50)
textField.pack(fill=tk.NONE, side=tk.TOP)

# create button to open file
openBtn = tk.Button(win, text='Open', command=load)
openBtn.pack(expand=tk.FALSE, fill=tk.X, side=tk.TOP)

# create button to save file
saveBtn = tk.Button(win, text='Save', command=save)
saveBtn.pack(expand=tk.FALSE, fill=tk.X, side=tk.TOP)

win.mainloop()
2 of 4
0

Remove the 'event' arguments from both function defs and remove brackets from your commands.

๐ŸŒ
Python Forum
python-forum.io โ€บ thread-35863.html
tkinter auto press button
December 24, 2021 - hi, sorry for my bad english, i have this case: import tkinter TheWindow = tkinter.Tk() def AddOne(): value = int(int(TextBox.get()) + 1) TextBox.delete(0, tkinter.END) TextBox.insert(0, str(value)) def RemOne(): value = int(int(Te...
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ tkinter โ€บ python-tkinter-basic-exercise-7.php
Python Tkinter event handling: Button clicks
August 19, 2025 - When the button is clicked, it calls the "on_button_click()" function, which changes the text of the label to "Button Clicked!". ... Previous: Python Tkinter GUI program: Adding labels and buttons.
๐ŸŒ
All About Circuits
forum.allaboutcircuits.com โ€บ home โ€บ forums โ€บ embedded & programming โ€บ programming & languages
Python tkinter detecting which button is pressed | All About Circuits
November 26, 2020 - for x in range(0,len(self.Serial_order)):# CREATE A LIST OF ARRAYS BASED ON CURRENT CSV FILE AND DISPLAY self.serial_entries.append(tk.StringVar()) self.serial_entries[x].set(self.Serial_order[x]) self.order_entries.append(tk.StringVar()) self.order_entries[x].set(self.Picking_order[x]) self.delete_entry_button.append(tk.Button(self.innercanvas,foreground="green",text="Delete entry",command =lambda: self.delete_row_of_entry(x))) code_labels_entry = tk.Entry(self.innercanvas,width=5,textvariable=self.serial_entries[x]) order_labels_entry = tk.Entry(self.innercanvas,width=5,textvariable=self.ord
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ python โ€บ tkinter โ€บ button โ€บ command
Tkinter Button command - Call Function on Button Click
November 30, 2020 - Tkinter Button command option sets the function or method to be called when the button is clicked.
๐ŸŒ
GitHub
gist.github.com โ€บ zed โ€บ 11390111
leave-button-pressed-ttk.py ยท GitHub
leave-button-pressed-until-stop.py ยท This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ยท
Top answer
1 of 5
11

Set a flag when the button is pressed, unset the flag when the button is released. There's no need for a loop since you're already running a loop (mainloop)

from Tkinter import * 
running = False
root = Tk()
def start_motor(event):
    global running
    running = True
    print("starting motor...")

def stop_motor(event):
    global running
    print("stopping motor...")
    running = False

button = Button(root, text ="forward")
button.pack(side=LEFT)
button.bind('<ButtonPress-1>',start_motor)
button.bind('<ButtonRelease-1>',stop_motor)
root.mainloop()

Assuming that you actually want to do something while the key is pressed, you can set up an animation loop using after. For example, to call a print statement once a second while the button is pressed you can add a function that does the print statement and then arranges for itself to be called one second later. The stop button merely needs to cancel any pending job.

Here's an example. The main difference to the original code is the addition of a move function. I also added a second button to show how the same function can be used to go forward or backward.

from Tkinter import * 
running = False
root = Tk()
jobid = None

def start_motor(direction):
    print("starting motor...(%s)" % direction)
    move(direction)

def stop_motor():
    global jobid
    root.after_cancel(jobid)
    print("stopping motor...")

def move(direction):
    global jobid
    print("Moving (%s)" % direction)
    jobid = root.after(1000, move, direction)

for direction in ("forward", "backward"):
    button = Button(root, text=direction)
    button.pack(side=LEFT)
    button.bind('<ButtonPress-1>', lambda event, direction=direction: start_motor(direction))
    button.bind('<ButtonRelease-1>', lambda event: stop_motor())

root.mainloop()
2 of 5
2

You might want to try the repeatinterval option. The way it works is a button will continually fire as long as the user holds it down. The repeatinterval parameter essentially lets the program know how often it should fire the button if so. Here is a link to the explanation:

http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/button.html

Search in-page for "repeatinterval".

Another name for this parameter is repeatdelay.