Site icon DataFlair

How to Use Multiple Button in GUI Application in Python

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Let’s explore making graphical programs in Python using Tkinter, with a special focus on using more than one button. Think of buttons as clickable things on the screen that do something when you press them. Our exploration aims to elucidate the process of incorporating and configuring multiple buttons within a GUI, enhancing user interaction and functionalities.

As we embark on this journey, we’ll unravel the simplicity of integrating buttons, paving the way for an enriched and interactive user experience. Whether it’s executing tasks or navigating through various features, these buttons become the keystones of functionality within our Tkinter GUI.

Topic Explanation:

In the initial phase, we’ll explore the fundamental syntax for creating buttons and associating functions with their click events. These buttons can be customized in terms of appearance, such as adjusting their size and color and even adding images for a visually appealing interface. Moving forward, we delve into the use of event handling to execute specific tasks when each button is pressed, enabling diverse functionalities.

We’ll uncover the flexibility of buttons, allowing us to implement features like navigation, data input, or any other custom action based on user interaction. Understanding these aspects equips us to create dynamic and responsive GUI applications with multiple buttons, tailoring the user experience to the specific needs of our Python program.

Prerequisite:

Code With Comments:

# Import the entire Tkinter module
from tkinter import *

# Function to change frame color based on button click
def btnClick(n):
    if n == 1:
        mf["bg"] = 'red'
    elif n == 2:
        mf["bg"] = 'green'
    elif n == 3:
        mf["bg"] = 'blue'
    elif n == 4:
        mf["bg"] = 'black'

# Create a Tkinter root window
my_root = Tk()

# Set the title for the root window
my_root.title("Button Event Code")

# Set a custom icon for the root window
my_root.wm_iconbitmap('2.ico')

# Create a frame with specific dimensions and background color
mf = Frame(my_root, width=700, height=700, bg='yellow', cursor='cross')
mf.propagate(0)
mf.pack()

# Create buttons with different colors and associate them with the btnClick function
btnred = Button(mf, width=7, height=2, text="Red Color", bg='red', fg='white', command=lambda: btnClick(1))
btngreen = Button(mf, width=9, height=2, text="Green Color", bg='green', fg='white', command=lambda: btnClick(2))
btnblue = Button(mf, width=9, height=2, text="Blue Color", bg='blue', fg='white', command=lambda: btnClick(3))
btnblack = Button(mf, width=9, height=2, text="Black Color", bg='Black', fg='white', command=lambda: btnClick(4))

# Pack the buttons into the frame
btnred.pack()
btngreen.pack()
btnblue.pack()
btnblack.pack()

# Start the Tkinter event loop
my_root.mainloop()

Output:

Code Explanation:

Conclusion

In wrapping up our journey through the incorporation of multiple buttons in Python Tkinter GUIs, we’ve discovered the exciting potential for crafting interactive and feature-rich applications. The versatility of having multiple buttons enables users to seamlessly interact with various aspects of the program. Reflecting on this exploration, it becomes clear that the ability to design interfaces that are intuitive and responsive, with a range of functionalities, sets the stage for continued advancements in Python GUI development.

Exit mobile version