1 - The graphical User Interface (GUI) tkinter for Python
So far, we have only seen the Python programming under console, we can then wonder how can we manipulate windows with buttons, labels and input fields ...
Well, know that the developers of the Python language, have already thought about that. Python already integrates with its interpreter a free graphic library named Tkinter. Creating your Human Machine Interfaces with this library will allow you to have no library to download in addition to your code.
But if you want to integrate other graphic libraries, here are the most famous bibs:
- Tkinter for Tk
- wxPython for wxWidgets
- PyGTK for GTK +
- PyQt for Qt.
Note that each of these libraries has its own characteristics. As for us in this tutorial, we will limit ourselves to tkinter which is integrated in the Python language.
2 - Create a window with tkinter
2 - 1 - Create a simple window with tkinter
- Import the Tkinter module.
- Create the main window of the GUI application.
- Add one or more of the widgets mentioned above to the GUI application.
- Enter the main event loop, mainloop()
Here's the code that create a simple window:
from tkinter import *
my_window = Tk()
my_window.mainloop()
What displays after execution:
2 - 2 - Add a name to the window
To add a name to the window just add the instruction:
my_window.title ("my first window")
Here is the code that creates a window with a name: "my first window"
from tkinter import *
my_window = Tk()
my_window.title("my first window")
my_window.mainloop()
What displays after execution:
2 - 3 - Define the dimensions of the tkinter window
To define the dimensions of the window, just use the geometry() method by adding the statement:
my_window.geometry ("width x height")
Here is the code that displays a window named "my first window" and dimension 350 x 200 :
from tkinter import *
my_window = Tk()
my_window.title("my first window")
my_window.geometry( "350x200")
my_window.mainloop()
What displays after execution:
3 Tkinter widgets
The Tkinter library provides various controls, such as buttons, labels, and text boxes used in a graphics application. These controls are commonly called widgets.
There are currently 15 types of widgets in Tkinter. Here we present the names of these widgets and a brief description:
- Button: the Button widget allows you to create buttons for your application.
- Canva: the Canva widget allows you to draw shapes, such as lines, ovals, polygons and rectangles, in your application.
- Checkbutton: the Checkbutton widget allows you to display a number of options in the form of check boxes. The user can select several options at the same time.
- Entry: the Entry widget is used to display a single line text field for accepting values from a user.
- Frame: the Frame widget is used as a container widget to organize other widgets.
- Label: the Label widget is used to provide a legend or description for the other widgets. It can also contain images.
- Listbox: The Listbox widget is used to provide a list of options to a user.
- menubutton: the menubutton widget is used to display the menus in your application.
- Menu: The Menu widget is used to provide various commands to a user. These commands are contained in Menubutton.
- Message: the Message widget is used to display multi-line text fields allowing to accept the values of a user.
- Radiobutton: the Radiobutton widget is used to display a number of options in the form of radio buttons. The user can only select one option at a time.
- Scale: The Scale widget is used to provide a cursor widget.
- Scrollbar: The Scrollbar widget is used to add scrolling functionality to various widgets, such as list boxes.
- Text: the Text widget is used to display text on multiple lines.
- Toplevel: the Toplevel widget is used to provide a separate window container.
- Spinbox: the Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select a fixed number of values.
- PanedWindow: the PanedWindow widget is a container that can contain any number of panes, arranged horizontally or vertically.
- LabelFrame: a labelframe is a simple container widget. Its main purpose is to act as a divider or container for complex window layouts.
- tkMessageBox: this module is used to display message boxes in your applications.
my-courses.net
This is my very first time that I am visiting here and I’m truly pleasurable to see everything at one place.
ux design studios
He came out inside an hour and helped us move our vintage dryer and mounted the shutoff valve.
Bay Area web design firms
The same trouble then we started out taking the steerage with UI consulting feels a great deal more comfortable than ever before!
best logo company
Very nice article which helped me a lot! Thank you so much !
Python Courses
Wonderful, just what a blog it is! This blog has provided the helpful data to us continue the good work.
brand building agency