Basics of GUI Programming
Basics of GUI Programming
GUI is a desktop app which helps you to interact with the computer.
Python provides several different options for writing GUI-based programs. These are listed below:
o Tkinter: It is easiest to start with. Tkinter is Python's standard GUI (graphical user interface)
package. It is the most commonly used toolkit for GUI programming in Python.
o JPython: It is the Python platform for Java that is providing Python scripts seamless access o Java
class Libraries for the local machine.
o wxPython: It is an open-source, cross-platform GUI toolkit written in C++. It is one of the
alternatives to Tkinter, which is bundled with Python.
There are many other interfaces available for GUI. But these are the most commonly used ones. In this,
we will learn about the basic GUI programming using Tkinter.
Tkinter:
Tkinter is a Python library that allows developers to create GUI applications quickly and easily. Tk provides
the following widgets:
o button
o canvas
o combo-box
o frame
o level
o check-button
o level-frame
o menu
o list - box
o menu button
o message
o tk_optoinMenu
o progress-bar
o radio button
o scroll bar
o separator
o tree-view, and many more.
Button:
Syntax:
btn=Button(master, option=value)
We have many other parameters that set the features of the button.
o activebackground: This parameter sets the background color when the cursor is on the button
o activeforeground: This parameter sets the foreground color when the cursor is on the button
o bg: We give the background color of the button to the parameter
o command: We give a function to this parameter. This function contains the code to set the
functionality of the button.
o font: This parameter sets the font style of the label of the button
o image: This parameter sets the background of the button to the image given
o width: This sets the width of the button to the given value
o height: This sets the height of the button to the given value
Example:
Output:
o Dimensions
o Fonts
o Colors
o Cursors
o Anchors
o Bitmaps
Canvas:
Canvas use to draw shapes such as lines, polygons, etc., and other layouts like graphics, text, and widgets.
Syntax:
can=Canvas(master, option=value)
The parent window is given as the parameter master. In addition, we have parameters like:
o bd: This parameter sets the border width. The value should be given in pixels
o bg: We give the background color of the canvas to this parameter
o cursor: This sets the cursor used when the cursor is on the canvas
o highlightcolor: This parameter sets the color of the focus highlight
o width: This sets the width of the canvas to the given value
o height: This sets the height of the canvas to the given value
Example:
Output:
CheckButton is display multiple options for the user to select. The user can select any number of
checkboxes.
Syntax:
cb=CheckButton(master, option=value)
The parent window is given as the master. There are many other parameters related to the check button like:
o activebackground: This parameter sets the background color when the cursor is on the check
button.
o activeforeground: This parameter sets the foreground color when the cursor is on the check button.
o bg: We give the background color of the check button.
o command: This takes a function that contains the code to set the functionality of the check button.
o font: This parameter sets the font style of the label of the button.
o image: This sets the input image on the check button.
Example:
Output:
This widget allows you to take single-line input from the user.
Syntax:
ent=Entry(master, option=value)
The parent window is given as the master. Besides, it has many other parameters like:
Example:
Output:
This widget acts as a container of other widgets. This is used to organize and position the widgets.
Syntax:
frame=Frame(master, option=value)
The parent window is given as the ‘master’ parameter. There are other parameters like:
Example:
Output:
This widget lets us give a list of items to the user to choose from.
Syntax:
lb=Listbox(master, option=value)
The parent window is given as the ‘master’ parameter. There are other parameters like:
Example:
Output:
This lets us add menus to the GUI. We can add a function to the menu button to open the menu options.
Syntax:
mb=MenuButton(master, option=value)
The parent window is given as the ‘master’ parameter. Other parameters include:
o activebackground: This parameter sets the background color when the cursor is on the menu button.
o activeforeground: This parameter sets the foreground color when the cursor is on the menu button.
o bd: This parameter sets the border width in pixels.
o bg: We can set the background color of the menu button using this parameter.
o cursor: This sets the cursor used in the menu button.
o highlightcolor: This parameter sets the color of the focus highlight.
o image: This parameter sets the background image of the menu button.
o width: This sets the width of the menu button.
o height: This sets the height of the menu button.
Example:
Output:
Syntax:
mn=Menu(master, option=value)
The parent window is given as the ‘master’ parameter. In addition, we can set other parameters like:
o activebackground: This parameter sets the background color when the cursor is on the menu.
o activeforeground: This parameter sets the foreground color when the cursor is on the menu.
o bg: We can set the background color of the menu using this parameter.
o command: This takes a function that contains the code to set the functionality of the menu.
o font: This parameter sets the font style of the text of items in the menu.
o image: This parameter sets the background image of the menu.
o title: This sets the title of the menu.
Example:
Output:
This widget lets us give multiple options to the user and he/she can choose one of the options.
Syntax:
rb=Radiobutton(master, option=value)
The parent window is given as the ‘master’ parameter. The other parameters that let us set other features
are:
o activebackground: This parameter sets the background color when the cursor is on the radio button.
o activeforeground: This parameter sets the foreground color when the cursor is on the radio button.
o bg: We can set the background color of the radio button using this parameter.
o command: This takes a function that contains the code to set the functionality of the radio button.
o font: This parameter sets the font style if the items in the radio button.
o image: This parameter sets the background image of the radio button widget.
o width: This sets the width of the radio button.
o height: This sets the height of the radio button.
Example:
Output:
This provides a slider that allows the user to slide any value.
Syntax:
sc=Scale(master, option=value)
The parent window is given as the ‘master’ parameter. The other parameters that let us set other features
are:
o activebackground: This parameter sets the background color when the cursor is on the scale
o bg: We can set the background color of the scale using this parameter
o cursor: This sets the cursor used in the scale
o from: This sets the value of one end of the scale
o image: This parameter sets the background image of the scale
o orient: Set the orientation of the widget to either HORIZONTAL or VERTICAL. By default, it is
vertical.
o to: It sets the value of the other end of the scale.
o width: This sets the width of the scale
Example:
Output:
This widget gives the scrolling feature to the other widgets like lists.
Syntax:
sb=Scrollbar(master, option=value)
The parent window is given as the ‘master’ parameter. The other parameters that let us set other features
are:
o activebackground: This parameter sets the background color when the cursor is on the scrollbar.
o bd: This parameter sets the border around the scrollbar.
o bg: We can set the background color of the scrollbar.
o cursor: This sets the cursor used when the cursor is on the scrollbar.
o width: This sets the width of the scrollbar.
Example:
Output:
This widget provides a separate container window. This does not need a parent window.
Syntax:
tl=TopLevel(master, option=value)
The parent window is given as the ‘master’ parameter. The other parameters that let us set other features
are:
o bd: This parameter sets the border around the top level.
o bg: We can set the background color of the top level.
o cursor: This sets the cursor used when the cursor is on the top level.
o height: This sets the height of the widget.
o width: This sets the width of the top level.
Example:
Output:
This is a widget that acts as a container of multiple panes oriented horizontally or vertically.
Syntax:
pw=PannedWindow(master, option=value)
The parent window is given as the ‘master’ parameter. The other parameters that let us set other features
are:
Example:
Output:
Output: