0% found this document useful (0 votes)
2 views15 pages

Basics of GUI Programming

The document provides an overview of GUI programming in Python, focusing on the Tkinter library, which is the standard toolkit for creating graphical user interfaces. It details various widgets available in Tkinter, such as buttons, checkboxes, and menus, along with their syntax and parameters. Additionally, it outlines methods for geometry management and includes examples of how to implement basic GUI applications.

Uploaded by

thatdumb67
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

Basics of GUI Programming

The document provides an overview of GUI programming in Python, focusing on the Tkinter library, which is the standard toolkit for creating graphical user interfaces. It details various widgets available in Tkinter, such as buttons, checkboxes, and menus, along with their syntax and parameters. Additionally, it outlines methods for geometry management and includes examples of how to implement basic GUI applications.

Uploaded by

thatdumb67
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Basics of GUI Programming:

GUI is a desktop app which helps you to interact with the computer.

Python Libraries for GUI Programming:

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.

Creating a GUI program using this Tkinter library.

For this, programmers need to follow the steps mentioned below.

o Import the module Tkinter


o Build a GUI application (as a window)
o Add those widgets that are discussed above
o Enter the primary, i.e., the main event's loop for taking action when the user triggered the event.

Prepared by Nawaraj Prajapati (MCA From JNU)


Example:
Output:

Button:

To add a button to the window, we use the Button() method.

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:

Prepared by Nawaraj Prajapati (MCA From JNU)


Standard Attributed for GUI:

o Dimensions
o Fonts
o Colors
o Cursors
o Anchors
o Bitmaps

Methods for Geometry Management:

o pack(): This method manages the geometry of widgets in blocks.


o grid(): This method organizes widgets in a tabular structure.
o place(): This method organizes the widgets to place them in a specific position.

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:

Prepared by Nawaraj Prajapati (MCA From JNU)


CheckButton:

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:

Prepared by Nawaraj Prajapati (MCA From JNU)


Entry:

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:

o bd: This parameter sets the border width in pixels


o bg: We can set the background color of the entry using this parameter
o cursor: This sets the cursor used when the cursor is on the entry
o command: This takes a function as an argument that gets called.
o highlightcolor: This parameter sets the color of the focus highlight
o width: This sets the width of the entry to the given value
o height: This sets the height of the entry to the given value

Example:

Output:

Prepared by Nawaraj Prajapati (MCA From JNU)


Frame:

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:

o bd: This parameter sets the border width in pixels.


o bg: We can set the background color of the frame using this parameter.
o cursor: This sets the cursor used when the cursor is on the frame.
o highlightcolor: This parameter sets the color of the focus highlight.
o width: This sets the width of the frame.
o height: This sets the height of the frame.

Example:

Output:

Prepared by Nawaraj Prajapati (MCA From JNU)


Listbox:

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:

o bd: This parameter sets the border width in pixels


o bg: We can set the background color of the Listbox using this parameter
o font: This parameter sets the font style of the text of items in the Listbox
o highlightcolor: This parameter sets the color of the focus highlight
o image: This parameter sets the background image of the Listbox
o width: This sets the width of the listbox
o height: This sets the height of the Listbox

We can use the insert() function to add elements to the Listbox.

Example:

Output:

Prepared by Nawaraj Prajapati (MCA From JNU)


MenuButton:

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:

Prepared by Nawaraj Prajapati (MCA From JNU)


Menu:

This widget allows us to create all kinds of menus in the application.

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:

Prepared by Nawaraj Prajapati (MCA From JNU)


Radiobutton:

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:

Prepared by Nawaraj Prajapati (MCA From JNU)


Scale:

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:

Prepared by Nawaraj Prajapati (MCA From JNU)


Scrollbar:

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:

Prepared by Nawaraj Prajapati (MCA From JNU)


TopLevel:

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:

Prepared by Nawaraj Prajapati (MCA From JNU)


PannedWindow:

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:

o bd: This parameter sets the border around the PannedWindow.


o bg: We can set the background color of the PannedWindow.
o cursor: This sets the cursor used when the cursor is on the PannedWindow.
o width: This sets the width of the PannedWindow.

Example:

Output:

Prepared by Nawaraj Prajapati (MCA From JNU)


Example for Sum of two numbers:

Output:

Prepared by Nawaraj Prajapati (MCA From JNU)

You might also like