A Graphical User Interface allows the user to interact with the application created on different platforms. GUI interfaces use different indicators like audio indicators, graphical icons, different widgets which makes it highly interactive and user friendly rather than Command-Line applications which are not visually appealing and are text-based interactions.
Tkinter provides a GUI look to the standard python interface. It comes pre-installed with the standard versions of Python on Windows, Linux, and macOS. Tkinter is a Python binding to the Tk GUI toolkit which is why it is named Tkinter. It is the most commonly used python GUI toolkit due to a large variety of widgets it supports and its ease of use.
Tkinter provides powerful GUI based widgets and functions which create a visually appealing and highly creative application in just a few lines of codes. Tkinter is famous for creating a GUI application because it opens up in a new window where the user can interact with the application.
In this article, we will explore how we can create a GUI application with a variety of widgets that are available in Tkinter.
Implementation of Tkinter Python GUI Toolkit
As Tkinter comes pre-installed with standard python installation so we will not be installing it although if you don’t have it installed you can install it using pip install tkinter.
- Importing required libraries
We will create a form using Tkinter and the widgets it provides. So we will import Tkinter. Also, we will create a window that will initiate the Tk class.
import tkinter as tk
window = tk.Tk()
- Creating a form step by step
Now we will create the form using different widgets and wrapping them in a single loop.
- Setting the Title
We will start by setting the turtle of the window that will run our form. As I already mentioned that we need everything in a single loop so that everything displays at one go we will create the main loop and define all our widgets and functions before that.
window.title('Article Submission Form')
window.mainloop() #this will be the end of our form to wrap everything
This is the basic layout of the window we created with the title as we mentioned in the code.
- Adding Label and Textbox
We will start by adding Labels for different sections and adding a text box to those labels to the user input. We will also make the application window a bit large in size so that we can see everything clearly without maximizing. Here we will use the ‘place’ function which takes the X and Y coordinate values and displays the widgets accordingly.
#Setting the Label
label = tk.Label(window, text = "Welcome to Analytics India Magazine").pack()
#Setting Window Size
name = tk.Label(window, text = "Name").place(x = 30,y = 50)
email = tk.Label(window, text = "Email").place(x = 30, y = 90)
phone = tk.Label(window, text = "Ph. Number").place(x = 30, y = 130)
#creating text box boxes for the labels
a1 = tk.Entry(window).place(x = 80, y = 50)
a2 = tk.Entry(window).place(x = 80, y = 90)
a3 = tk.Entry(window).place(x = 100, y = 135)
- Adding Radio Button
Now we will create radio buttons to take the user input for ‘Gender. We use radio buttons when the user is allowed to select only one option.
gender = tk.Label(window, text = "Gender").place(x = 30, y = 165)
radio1 = tk.Radiobutton(window, text="Male").place(x = 80, y = 165)
radio2 = tk.Radiobutton(window, text="Female", state='disabled').place(x =80,y = 185)
radio3 = tk.Radiobutton(window, text="Other", state='disabled').place(x = 80, y = 205)
- Adding Checkbox
We will create a skills section and add a checkbox for different skills. Checkboxes are used when users are allowed to select more than one value.
skill = tk.Label(window, text = "Skills").place(x = 30, y = 225)
check1 = tk.Checkbutton(window, text = "Artificial Intelligence", height =1, width = 15).place(x= 80, y = 225)
check2 = tk.Checkbutton(window, text = "Machine Learning", height = 1, width = 13).place(x= 80, y = 245)
check3 = tk.Checkbutton(window, text = "Data Science", height = 1,width = 9).place(x= 80, y = 265)
- Creating Button
This is the final step where we will create the submit button. Also, we will create a pop-up window when we click the submit button.
from tkinter import messagebox
messagebox.showinfo("Thank You","Our Team Will Get Back to you")
click1 = tk.Button(window,text = "Submit", command = onClick).place(x= 150, y=380)
Now when we click the submit button the onclick function will be called and a message box will be displayed.
In the image above you can see how a pop-up window is opened with the title and message as we defined.
In this article initially, we started with creating a basic layout window using the Tkinter. After that, we explored different widgets that are there in the Tkinter library and used them into creating a form. We saw how we can place different widgets on the application window using the place function and in the end, we created a pop-up window that opens as soon as we click submit.
This is how you can create beautiful applications using many more widgets that are there in Tkinter in just a few lines of code.