In Tkinter, button widgets are often used to trigger functions or actions when they are clicked. You can associate a function with a button by using the `command` option. Here's how to create a button and link it to a function:
```python
import tkinter as tk
def button_click():
# This function will be called when the button is clicked
print("Button clicked!")
# Create a Tkinter window
window = tk.Tk()
window.title("Button Example")
# Create a button and link it to the button_click function
button = tk.Button(window, text="Click Me", command=button_click)
button.pack()
# Start the Tkinter event loop
window.mainloop()
```
In this example:
1. We create a button using `tk.Button`, set its text using the `text` option, and link it to the `button_click` function using the `command` option.
2. When the button is clicked, the `button_click` function is called, and it prints "Button clicked!" to the console.
3. The `window.mainloop()` function starts the Tkinter event loop, which is necessary for your GUI to respond to user interactions, such as button clicks.
You can replace the `button_click` function with any custom function you want to execute when the button is clicked. This allows you to perform various tasks, such as opening files, saving data, or updating the interface.