In this tutorial, you are going to learn how to create GUI applications in Python. You’ll also learn about all the elements needed to develop GUI apps in Python.
Graphical User Interface(GUI) with tkinter
GUI is a desktop app which helps you to interact with the computers. They are used to perform different tasks in the desktops, laptops, other electronic devices, etc..,
- GUI apps like Text-Editors are used to create, read, update and delete different types of files.
What is tkinter
Python offers multiple options for developing Graphical User Interface(GUI). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit with Python.
Steps to follow:
- Importing the module – Tkinter
- Create the main window (container)
- Add any number of widgets to the main window
- Apply the event Trigger on the widgets.
There are two main methods used you the user need to remember while creating the Python application with GUI.
- Tk(): To create the main window, Tkinter offers a method TK(). The basic code used to create the main window of the application is:
m=tkinter.Tk() where m is the name of the main window object
2.mainloop(): mainloop() is used when you are ready for the application to run. mainloop() is an infinite loop used to run the application, wait for an event to occur and process the event till the window is not closed.
import tkinter w = tkinter.Tk() w.title("GUI") label = tkinter.Label(w, text = "Hello World!").pack() w.mainloop()
Geometry Management in tkinter
All widgets in the tkinter will have some geometry measurements. These measurements give you to organize the widgets and their parent frames, windows, etc..,
Tkinter has the following three Geometry Manager classes.
- pack():- It organizes the widgets in the block, which mean it occupies the entire available width. It’s a standard method to show the widgets in the window
pack() and grid() method of Tkinter in Python
- grid():- It organizes the widgets in table-like structure. You will see details about grid later in this tutorial.
- place():- It’s used to place the widgets at a specific position you want.
Widgets are something like elements in the HTML. Let’s see
- Button: Button widget is used to place the buttons in the tkinter.
- Canvas: Canvas is used to draw shapes, such as lines, ovals, etc.., in your GUI application.
- Checkbutton: Checkbutton is used to create the check buttons in your application. You can select more than one option at a time.
- Radiobutton:-The Radiobutton widget is used to display a number of options as radio buttons. The user can select only one option at a time.
- Entry: Entry is used to input the single-line text entry from the user. For multi-line text input, Text widget is used.
- Text: To edit a multi-line text and format the way it has to be displayed.
- Frame: Frame is used as containers in the tkinter. It is used for grouping and organizing the widgets.
- Label: Label is used to create a single line widgets like text, images, etc..,
import tkinter window = tkinter.Tk() window.title("GUI") tkinter.Label(window, text = "Username").grid(row = 0) tkinter.Entry(window).grid(row = 0, column = 1) tkinter.Label(window, text = "Password").grid(row = 1) tkinter.Entry(window).grid(row = 1, column = 1) tkinter.Checkbutton(window, text = "Keep Me Logged In").grid(columnspan=2) window.mainloop()
- Menu:- Menu is used to create all kinds of menus in the GUI.
- MenuButton:-It is a part of top-down menu which stays on the window all the time. Every menubutton has its own functionality.
- Listbox: It offers a list to the user from which the user can accept any number of options.
import tkinter window = tkinter.Tk() list=Listbox(window) list.insert(1,'Python') list.insert(2,'Java') list.insert(3,'C++') list.insert(4,'Any other') list.pack() window.mainloop()
- Message: It refers to the multi-line and non-editable text. It works the same as that of Label.
import tkinter window = tkinter.Tk() messagebox=Message(window,text="This is our Message") messagebox.config(bg='lightgreen') messagebox.pack() window.mainloop()
- Scale: It is used to provide a graphical slider that allows selecting any value from that scale.
- Scrollbar: It refers to the slide controller which will be used to implement listed widgets.
- SpinBox: It is an entry of ‘Entry’ widget. Here, the value can be input by selecting a fixed value of numbers
So Guy’s, I hope you really enjoy this tutorial and feel free to leave a comment if you have any doubt.
You may also learn: