Python Tkinter Button

Python Tkinter Button

Python Tkinter Button

The Button widget in Tkinter is used to add interactive buttons to a Python application. Tkinter allows developers to configure the appearance and behavior of buttons based on their requirements. Various attributes can be set or modified dynamically to enhance the user interface.

One of the key features of the Tkinter Button widget is that it can be associated with a function or method, which is triggered when the button is clicked.

Complete Python Course with Advance topics:-Click here

 

Syntax:

W = Button(parent, options)

Here, parent refers to the parent window or frame where the button will be placed, and options represent various parameters that define the appearance and functionality of the button.

Tkinter Button Options:

Below is a list of possible options that can be used to customize a Button in Tkinter:

SN Option Description
1 activebackground Background color when the mouse hovers over the button.
2 activeforeground Font color when the mouse hovers over the button.
3 bd Border width in pixels.
4 bg Background color of the button.
5 command Function to be called when the button is clicked.
6 fg Foreground (text) color of the button.
7 font Font style of the button text.
8 height Height of the button in lines (text) or pixels (image).
9 highlightcolor Color of the highlight when the button is focused.
10 image Image to be displayed on the button.
11 justify Text alignment (LEFT, RIGHT, CENTER).
12 padx Horizontal padding inside the button.
13 pady Vertical padding inside the button.
14 relief Border style (SUNKEN, RAISED, GROOVE, RIDGE).
15 state Button state (DISABLED, ACTIVE).
16 underline Underlines a character in the button text.
17 width Width of the button in characters or pixels.
18 wraplength Wraps text to fit within the specified length.

Example 1: Creating a Simple Button

The following example demonstrates how to create a basic button in Tkinter:

from tkinter import *

top = Tk()
top.geometry("200x100")

b = Button(top, text="Simple")
b.pack()

top.mainloop()

Output:

A simple button with the label “Simple” appears in the application window.

image-7 Python Tkinter Button


Example 2: Creating Multiple Buttons with Different Attributes

Below is an example demonstrating multiple buttons with different colors and functionalities:

from tkinter import *
from tkinter import messagebox

top = Tk()
top.geometry("250x150")

def fun():
    messagebox.showinfo("Hello", "Red Button clicked")

b1 = Button(top, text="Red", command=fun, activeforeground="red", activebackground="pink", pady=10)
b2 = Button(top, text="Blue", activeforeground="blue", activebackground="pink", pady=10)
b3 = Button(top, text="Green", activeforeground="green", activebackground="pink", pady=10)
b4 = Button(top, text="Yellow", activeforeground="yellow", activebackground="pink", pady=10)

b1.pack(side=LEFT)
b2.pack(side=RIGHT)
b3.pack(side=TOP)
b4.pack(side=BOTTOM)

top.mainloop()

Output:

A window opens with four buttons—Red, Blue, Green, and Yellow—positioned at different sides of the window. When the Red button is clicked, a message box appears with the text: “Red Button clicked”.

image-8 Python Tkinter Button

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Conclusion

The Tkinter Button widget is a fundamental UI element that allows users to interact with a Python application. With its various configuration options, developers can enhance the aesthetics and functionality of buttons to create engaging graphical interfaces. By utilizing event handling, Tkinter buttons can perform different actions based on user interaction, making applications more dynamic and user-friendly.

For more Tkinter tutorials and updates, stay connected with UpdateGadh!


tkinter button command with arguments
how to make a button in python without tkinter
tkinter button styles
tkinter button documentation
tkinter button position
tkinter button example
tkinter button relief
python button command
python tkinter
tkinter button
python tkinter button list
python tkinter button example
python tkinter button
python tkinter button command
python tkinter button color
python tkinter button disable
python tkinter button multiple commands
python tkinter button size
python tkinter buttons side by side
python tkinter button style
python tkinter button example

Post Comment