Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to press a button without touching it on Tkinter?
The Tkinter button widget can be used to perform a specific actionable event within the application. We can also invoke the button widget without performing a click operation on it. The invoke() method in Tcl/Tk does the same thing which returns a string in case if there are any commands given to the Button. The invoke() method can be called up after the initialization of the Button widget. The event will be called automatically once the Button widget is prepared.
What is invoke() Method?
The invoke() method programmatically triggers a button's command without user interaction. It executes the same function that would run if the user clicked the button physically.
Syntax
button.invoke()
Example − Auto-trigger Button on Startup
Here's how to automatically press a button when the application starts ?
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
# Create an instance of tkinter frame
win = tk.Tk()
# Set the size of the tkinter window
win.geometry("700x350")
def display_msg():
messagebox.showinfo("Message", "Hello There! Greeting from TutorialsPoint.")
# Add a Button widget
b1 = ttk.Button(win, text="Click Me", command=display_msg)
b1.pack(pady=30)
# Automatically invoke the button
b1.invoke()
win.mainloop()
The output of the above code is ?
A popup message box appears automatically when the application starts. The button remains clickable for future interactions.
Example − Invoke Button After Delay
You can also trigger a button after a specific time delay using after() method ?
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
def create_delayed_button():
win = tk.Tk()
win.geometry("400x200")
win.title("Delayed Button Invoke")
def show_message():
messagebox.showinfo("Delayed", "Button pressed automatically after 3 seconds!")
button = ttk.Button(win, text="Auto-Press Button", command=show_message)
button.pack(pady=50)
# Invoke button after 3000 milliseconds (3 seconds)
win.after(3000, button.invoke)
win.mainloop()
create_delayed_button()
The output of the above code is ?
The application window opens. After 3 seconds, the button is automatically pressed. A message box appears without user interaction.
Common Use Cases
The invoke() method is useful for:
- Default Actions − Trigger default behavior on startup
- Automated Testing − Simulate button clicks programmatically
- Keyboard Shortcuts − Bind keys to trigger button actions
- Timed Events − Execute actions after delays
Conclusion
The invoke() method allows you to programmatically press Tkinter buttons without user interaction. Use it for auto-triggering actions on startup, creating timed events, or implementing automated workflows in your GUI applications.
