TKINTER (Python) Introduction
This blog is intended to be useful for anyone who
are interested in getting started the learning of tkinter. and using python to
create GUI applications. Tkinter is an open source, portable graphical user
interface (GUI) library designed for use in Python scripts.
Let begins with checking what actuially graphical
user interface is,
What
is Graphical user interface(GUI)?
Graphical user interface is nothing but a desktop
application which is used to interact with computers. they are used to perform
different tasks in laptops/computers. some of the graphical user interface
applications are (text editors , games, browsers).
It is kind of user interface where user can
interface with the computers using icons and visual indictors.
Python
in developing GUI’s:
Python has lots of libraries to create a graphical user interface, the top most used
are (kivi, pythonqt, wxpython, Tkinter).
Among these Tkinter is more popular and its is
preferred my most of the developers and the learners because of its simplicity
and way of use.
Basic python knowledge will help to work with
tkinter.
What
is Tkinter:
Tkinter is a built in python module which is used to
create simple gui tools.
It is the most commonly used module for creating a
GUI application using python. python combined with Tkinter provides fast and
easy way to develop GUI
It is the standard GUI library in python GUI programming.
Fundamentals of tkinter:
Creating GUI using TKinter is very easy, all you
need is perform the following steps.
- Creating the GUI application main window
- Adding the widgets
- Enter the event loop
Tkinter
Widgets:
A widget is an element ogf graphical user interface
that provides information or a specific way to interact with the applications.
Tkinter provides various controls, such as buttons, labels and text boxes used
in a GUI application. These controls are commonly called widgets.
Tkinter supports below mentioned widgets,
Label-It
is used to display text or image on the screen.
Button-It
is used to add buttons to your application.
Canvas-It
is used to draw pictures and others layouts like texts, graphics etc.
ComboBox-It
contains a down arrow to select from list of available options.
CheckButton-It
displays a number of options to the user as toggle buttons from which user can
select any number of options.
Radio
Button-It is used to implement one-of-many selection as it
allows only one option to be selected.
Entry-It
is used to input single line text entry from user.
Frame-It
is used as container to hold and organize the widgets.
Message-It
works same as that of label and refers to multi-line and non-editable text.
Scale-It
is used to provide a graphical slider which allows to select any value from
that scale.
Scrollbar-It
is used to scroll down the contents. It provides a slide controller.
SpinBox-It
is allows user to select from given set of values.
Text-It
allows user to edit multiline text and format the way it has to be displayed.
Menu-It
is used to create all kinds of menu used by an application.
Geometry
Management:
Tkinter also offers access to the geometric
configuration of the widgets which can organize the widgets in the parent
windows. There are mainly three geometry manager classes class.
pack()
:It organizes the widgets in blocks before placing in the parent widget.
grid()
:It organizes the widgets in grid (table-like structure) before placing in the
parent widget.
place()
:It organizes the widgets by placing them on specific positions directed by the
programmer.
Creating
widgets in python Tkinter:
Creating
a basic GUI:
Tkinter is a builtin library in python so you don’t
need to install it.you can create a basic gui by following the fundamentals
mentioned above.
By running this file you can create a basic gui as
below, the GUI will contain minimizing and maximizing window option and close
option , while you press the close button it will end the loop that’s how the
program stops.
.pack() packs the widgets in the centre of the
window ,even you resize the window it will automatically pack the widgets in
the centre.
Grid system used to pack the widgets in rows and
columns, you can define the widgets with different grids. even after the
resizing the window the widgets will be in the assigned grid only.
Creating
Buttons:
You can create a button by following similar method
for label, you can also set the theme by passing activebackground, bg, fg,
height , width.
You can also set a command for while activating the
button.
Creating
Entry Field:
It is used to input the single line text entry from
the user. For multi-line text input, Text widget is used. you can create entry widget by following below.
w
= Entry(master winsow, option=value)
first yo need to to write the syntax and define the
entry field width then you can define font type, color,background.
Creating
Check button:
To select any number of options by displaying a
number of options to a user as toggle buttons. The general syntax is:
w
= CheckButton(master window, option=value)
to create a check button first you need to define
the variables.and pass the the values inside the syntax.and set off value and
on values for the check buttons,you can also change the themes like
background,height, width.
Creating Radio Button:
It is used to offer multi-choice option to the user.
It offers several options to the user and the user has to choose one option.
The general syntax is:
w = RadioButton(master, option=value)
you can create radiobutton similarly like check button but you will define only one variable for multiple buttons.
Creating Combobox:
Combobox is a combination of Listbox and an entry
field. It is one of the Tkinter widgets where it contains a down arrow to
select from a list of options. It helps the users to select according to the
list of options displayed.
Syntax:
combobox
= ttk.Combobox(master, option=value, ...)
first you need to define the variable, then the
values in the syntax.
Creating
Frame:
A frame is a rectangular region on the screen. A
frame can also be used as a foundation class to implement complex widgets. It
is used to organize a group of widgets.
Syntax:
The syntax to use the frame widget is given below.
w = frame( master, options).
first you need to define the frame ,then pack the
frame inside the window, then create the widgets you need and pack them inside
the frame.
Creating
Menu:
Menus are the important part of any GUI. A common use of menus is to provide convenient access to various operations such as saving or opening a file, quitting a program, or manipulating data. Toplevel menus are displayed just under the title bar of the root or any other toplevel windows.
menu
= Menu(master, **options)
first you need to define the menu bar and menu bar
items. then define the items actions.
Creating
Scrollbar:
The scrollbar widget is used to scroll down the
content. We can also create the horizontal scrollbars to the Entry widget.
Syntax:
The syntax to use the Scrollbar widget is given below.
w = Scrollbar(master, options)
- Tkinter is easy and fast to implement as compared to any other GUI toolkit.
- Tkinter is more flexible and stable.
- Tkinter is included in Python, so nothing extra need to download.
- Tkinter provides a simple syntax.
- Tkinter is really easy to understand and master.
- Tkinter provides three geometry managers: place, pack, and grid. That is much more powerful and easy to use.
Comments
Post a Comment