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.

Example

# Import the required libraries
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

# Create an instance of tkinter frame
win = 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)
b1.invoke()

win.mainloop()

Output

Running the above code will show up a popup message box automatically. When we click the Button, the popup will appear from the main window.

Updated on: 08-Jun-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements