Creating Button with Tkinter Python | Creating GUI with Python

In Graphical User Interface Button Widgets plays an important role. The button is a graphical user component that is the most common way to trigger action from. we expect the user to click on the button to perform some tasks, in normal cases.

What are Widgets in Tkinter Python?

As I mentioned above button is the most important widget, in the graphical user interface? Now you might be thinking what is a widget in Tkinter Python? Let me answer this question a widget in a graphical user interface is all the things that are visible on the screen and are available on the screen. The best example of widgets are buttons, text labels, and other components. In this article, we will discuss the button widget in Tkinter Python Library.


Python Programming language is A very large number of Python Libraries. That can make your task even easier. Some of the Python Libraries that you should know are here.


The button Widget in Tkinter Python:

As another widget in Tkinter Python, the button can be considered one of the important and crucial widgets, which can help us control the action of the user. We can control things with the help of a button in Tkinter python.

For example, if we have a form that has to be filled by the user, now we expect the user to fill out the form and when they confirm all the information then the user should let the system know by clicking the submit button.

Creating a button with Tkinter Python:-

Tkinter gives us classes to create different widgets, and so is the case with a button. We have a BUtton class in tkk. which gives us the ability to create a button with the help of the Tkinter library. 

Below is the code that helps us create a simple button. 

    
from tkinter import Tk
from tkinter import ttk
root = Tk()
button = ttk.Button(root,text="AlixaProDev")
button.grid()
root.mainloop()
               

The above code generates a simple window, with one simple button on it, and the button text that we have specified is "alixaprodev". The button is now clickable but can not trigger any action. We will do that in a second but first, we have to look at the other option available for the button.

How to check the text of the button in Tkinter?

The attributes of the button are like a dictionary, which can be accessed by using the key. As we know that a dictionary in python is a data structure, where we have one key and value. The key in the dictionary should be unique. The current text of the button can be checked by the key "text". an example of the illustration is given below. 

    
from tkinter import Tk
from tkinter import ttk
root = Tk()
button = ttk.Button(root,text="AlixaProDev")
button.grid()
print(button["text"])
root.mainloop()

# output to the terminal 
# AlixaProDev 

How to change the text of a button on runtime in Tkinter Python?

Sometimes we want to change the text of a button at runtime. We can do so by using the button attribute "text". we can then assign anything we want. Look at the following example which will help you get to the point. 

In the following example, we want to change the text of a button to "alixaprodev.com" if the user input is equal to 1.

    
from tkinter import Tk
from tkinter import ttk
root = Tk()
button = ttk.Button(root,text="AlixaProDev")
button.grid()
print(button["text"])
inp=int(input())
if inp==1:
    button["text"]="alixaprodev.com"
root.mainloop()


How to Create a colorful button with Tkinter Python?

In order to create a colorful button, we can use the background property of the Tkinter button. which will help us create a background color for the button. in the below code, I have to change the default color to red. so the output of the code will be a red button with text.

    
from tkinter import Tk
from tkinter import Button
root = Tk()
button = Button(root,text="AlixaProDev",bg="red")
button.grid()
root.mainloop()



A list of 5 buttons with different colors. Below is the code of the 5 buttons with 5 different colors.

    
from tkinter import Tk
from tkinter import Button
root = Tk()
button1 = Button(root,text="AlixaProDev",bg="red").grid()
button2 = Button(root,text="AlixaProDev",bg="green").grid()
button3 = Button(root,text="AlixaProDev",bg="black").grid()
button4 = Button(root,text="AlixaProDev",bg="pink").grid()
button5 = Button(root,text="AlixaProDev",bg="blue").grid()
root.mainloop()


The output of the above code is 5 buttons with 5 different colors.

button in tkinter python

Learn Tkinter In Easy Way

This is how we can create a button with Tkinter Library Python. If you are interested in other Programming tutorials Please vistit dialy.

AlixaProDev

I’m Ali, Founder of alixaprodev.com. I am a Software developer, and I love to write articles to help students, developers, and learners. I started alixaprodev.com because of my love for Python, Java, and WebDev.

Post a Comment (0)
Previous Post Next Post