Python TKinter GUI Buttons

In this Python TKinter GUI article i want talk about GUI Buttons in TKinter, and also we are going to handle click events for the Button. so first of all let’s talk that What are TKinter Buttons?

 

 

What are TKinter Buttons?

Tkinter Buttons are graphical user interface (GUI) elements, and that is used in Python Tkinter library to create interactive buttons, using buttons users can click to trigger actions or events. Buttons typically appear as rectangular or square widgets with text or images on them.In Tkinter, Buttons are created using the Button widget class. They can be customized with different attributes such as text, font, colors, images, and command functions.

 

 

 

So this is the complete code for Python TKinter GUI Creating Buttons

In the above code first, we import tkinter and ttk, where ttk enhances the GUI’s appearance. Then, we create a window with a minimum size of 600×400 and set its title. Next, we create a label using ttk.Label() with specified text and position it in a grid layout. The clickMe() method establishes the relationship between the label and button, modifying the label’s text and color upon button click using label.configure(). We create a button with ttk.Button() and assign the clickMe() method to its command parameter. Finally, we start the main event loop for the window to handle user interactions.

 

 

 

 

Run the complete code and this will be the result

Python TKinter GUI Buttons
Python TKinter GUI Buttons

 

 

 

Add Images to TKinter GUI Buttons

Now we want to add images in our TKinter Buttons, Using images on buttons can enhance the visual appeal of a GUI and make the interface more beautiful for users. Let’s walk through a practical example of how to use images on buttons in a Tkinter application:

In this example, first we have imported the necessary modules from tkinter and pillow library, make sure that you have installed pillow library (pip install pillow).

After that we load an image file using PIL’s Image.open() function, we resize it to the desired dimensions, and then convert it to Tkinter PhotoImage object using ImageTk.PhotoImage(). This image will be used as the button’s icon.

After that we creates two buttons: image_button and text_image_button. image_button displays only the image, while text_image_button displays both the image and text. The compound=tk.LEFT option specifies that the image should be positioned to the left of the text on the button. Both buttons are configured with a command to call the clickMe() function when clicked. 

 

 

Run the complete code and this will be the result 

Python TKinter GUI Buttons
Python TKinter GUI Buttons

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×