GUI In Python.pdf By : Sangeeta M Chauhan , Gwalior

jonathanlimberestrad 48 views 26 slides Jun 14, 2024
Slide 1
Slide 1 of 26
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

Best Python GUI Frameworks which supports multiple platforms (Windows,
Linux and Mac). These all GUI frameworks are easy to use and popular,
some of them are open-source.


Slide Content

GUI IN PYTHON
By : SangeetaM Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

BestPythonGUIFrameworkswhichsupportsmultipleplatforms(Windows,
LinuxandMac).TheseallGUIframeworksareeasytouseandpopular,
someofthemareopen-source.
•Kivy:OpenGL ES 2, supports multiple platforms namely
Windows, MacOSX, Linux, Android iOSand Raspberry Pi. Open
source
•PyQT:Cross-platform ,Supports Unix/Linux, Windows, Mac OS X
and Sharp Zaurus. It is available in both, commercial as well as
GPL license. Although some features may not be available in the
free version, but you can use it under the free license with open
source.
•WxPythonopen source wrapper for cross-platform GUI library
WxWidgets(earlier known as WxWindows) and implemented as
a Python extension module. With WxPythonyou as a developer
can create native applications for Windows, Mac OS and Unix. If
you’re just beginning to develop applications in WxPython, here
is a good simpletutorialyou can go through.
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

•PyGUIcross-platformframeworkforUnix,Macintoshand
Windows.lightweight,.
•PySidefreeandcross-platformGUItoolkitQtinitiatedand
sponsoredbyNokia,,currentlysupportsLinux/X11,MacOSX,
MaemoandWindowsand,supportforAndroidisintheplans
forthenearfuture.PySideprovidestoolstoworkswith
multimedia,XMLdocuments,network,databasesandGUI.A
keyfeatureofPySideisitsAPIcompatibilitywithPyQt4,soif
youwishtomigratetoPySidethentheprocesswillbehassle-
free.
•TkinterbundledwithPython,Python’sstandardGUI
framework.PopularforitssimplicityandGUI.Opensource
andavailableunderthePythonLicense.
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

To create a tkinterapplication:
1.Import module –tkinter
2.Create the main window (container)
3.Add any number of widgets to the main
window
4.Apply the event Trigger on the widgets.
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Therearetwomainmethodstobeusedwhilecreating
applicationusingtkinter
•Tk(screenName=None, baseName=None, className=’Tk’, useTk=1):
This creates a toplevelwidget of Tkwhich usually is the main
window of an application. Each instance has its own associated Tcl
interpreter.
•Mainloop() : 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.
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

To create window
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

TkinterWidgets (Controls )
Sr.No. Operator & Description
1 Button
TheButtonwidgetisusedtodisplaybuttonsinyourapplication.
2 Canvas
TheCanvaswidgetisusedtodrawshapes,suchaslines,ovals,polygonsandrectangles,
inyourapplication.
3 Checkbutton
TheCheckbuttonwidgetisusedtodisplayanumberofoptionsascheckboxes.Theuser
canselectmultipleoptionsatatime.
4 Entry
TheEntrywidgetisusedtodisplayasingle-linetextfieldforacceptingvaluesfroma
user.
5 Frame
TheFramewidgetisusedasacontainerwidgettoorganizeotherwidgets.
6 Label
TheLabelwidgetisusedtoprovideasingle-linecaptionforotherwidgets.Itcanalso
containimages.
7 Listbox
TheListboxwidgetisusedtoprovidealistofoptionstoauser.
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Sr.No. Operator & Description
8 MenubuttonTheMenubuttonwidgetisusedtodisplaymenusinyourapplication.
9 MenuTheMenuwidgetisusedtoprovidevariouscommandstoauser.Thesecommands
arecontainedinsideMenubutton.
10 MessageTheMessagewidgetisusedtodisplaymultilinetextfieldsforacceptingvalues
fromauser.
11 RadiobuttonTheRadiobuttonwidgetisusedtodisplayanumberofoptionsasradio
buttons.Theusercanselectonlyoneoptionatatime.
12 ScaleTheScalewidgetisusedtoprovideasliderwidget.
13 ScrollbarTheScrollbarwidgetisusedtoaddscrollingcapabilitytovariouswidgets,such
aslistboxes.
14 TextTheTextwidgetisusedtodisplaytextinmultiplelines.
15 ToplevelTheToplevelwidgetisusedtoprovideaseparatewindowcontainer.
16 SpinboxTheSpinboxwidgetisavariantofthestandardTkinterEntrywidget,whichcan
beusedtoselectfromafixednumberofvalues.
17 PanedWindowAPanedWindowisacontainerwidgetthatmaycontainanynumberof
panes,arrangedhorizontallyorvertically.
18 LabelFrameAlabelframeisasimplecontainerwidget.Itsprimarypurposeistoactasa
spacerorcontainerforcomplexwindowlayouts.
19 tkMessageBoxThismoduleisusedtodisplaymessageboxesinyourapplications.
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Before learininghow to handle widgets lets understand
3 important manager classes
•pack() method:Itorganizes the
widgets in blocks before placing in
the parent widget.
•grid() method:Itorganizes the
widgets in grid (table-like
structure) before placing in the
parent widget.
•place() method:Itorganizes the
widgets by placing them on
specific positions directed by the
programmer.
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Button:Toadd a button in your application, this widget is used.
The general syntax is:
w=Button(master, option=value)
•Master:parameterusedtorepresenttheparentwindow.
•Options:Therearenumberofoptionswhichareusedtochange
theformatoftheButtons.Numberofoptionscanbepassedas
parametersseparatedbycommas.Someofthemarelistedbelow.
–activebackground:tosetthebackgroundcolorwhenbuttonisunder
thecursor.
–activeforeground:tosettheforegroundcolorwhenbuttonisunder
thecursor.
–bg:tosethenormalbackgroundcolor.
–command:tocallafunction.
–font:tosetthefontonthebuttonlabel.
–image:tosettheimageonthebutton.
–width:tosetthewidthofthebutton.
–height:tosettheheightofthebutton.
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Lets create a Button in window
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Button is
created

PACK() Geometry Manager
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
ThePackgeometrymanagerpackswidgetsinrowsorcolumns.
Wecanuseoptionslikefill,expand,andsidetocontrolthis
geometrymanager.
expand− When set to true, widget expands to fill any
space not otherwise used in widget's parent.
fill− Determines whether widget fills any extra space
allocated to it by the packer, or keeps its own minimal
dimensions: NONE (default), X (fill only horizontally), Y (fill
only vertically), or BOTH (fill both horizontally and
vertically).
side− Determines which side of the parent widget packs
against: TOP (default), BOTTOM, LEFT, or RIGHT.

PACK with Button
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
This examples
creates 4 Buttons
(without
functionality)
with text in
different colours

Pack() with expand option
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Button 1 is
expandedd
with frame

To change position of Button use grid function
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Positioning of Buttons in grid
Message box will be
displayed on click of “OK
Button

List of some basic controls (Widgets) of tkinter
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
w=Entry(parent,options) –For Text Field single line
w = Text (parent, option, ... )-For Text Field Multi line
w = Label (parent, option, ... )-For Label
w = Button (parent, option, ... )-For Button
w = Message (parent , option, ... )-For message Box
w = Checkbutton(parent, option, ... )-For CheckBox
w = Radiobutton(parent, option, ... )-For RadioButton
w = Listbox(parent , option, ... )-For List Box

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
OPTION VALUE EFFECT
foreground (fg)Colour Changes the foreground colour. (Colourmay be
specified using pre-defined keywords or using RGB
values)
background (bg)Colour Changes the background colour. (Colour may be
specified using pre-defined keywords or using RGB
values)
bd (border width)integer Specifies the width of the widget border
command CallbacktypeSpecifies which method is to be executed when the
widget is selected.
font Font typeSpecifies the font used by the widget. Often
represented in a tuple (family, size, weight)
padx, pady Integer Specifies the width between the current widget and the
neighbor widgets.
relief Relief type:
•GROOVE
•SUNKEN
•RIDGE
•FLAT
Specifies the relief of the widget.
text string Specifies the text appearing on the widget at run time
Some Common Options used with tkinterWidgets

Lets implement some of the widgets through examples
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
A function is defined to
display a message
when Button is clicked
Defining action : Button
will call Function
‘WelcomeMsg()” and
pass E1 object(TextField)

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
OUTPUT OF PREVIOUS PROGRAM
When User Clicks on Button
it will display a Message
Box

Lets create a List Box containing Python Topics
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
output

This example shows how to create Option Button and Radio Button
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
These functions
are showing
which Checkbox
or Radio Button
is Selected
………..Continued to next page

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
………..Continued from previous page
Variables are created to store values
for checkboxes and radiobuttons
Functions are called when
widgets(controls) are selected
Label is created to display
message using functions
sel(),selchk1(),selchk2()…..

Output
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Message displayed when radio button or
checkbox is selected

To View More Programs using tkinterfollow the link:
1. https://pythonclassroomdiary.wordpress.com/gui-
programstkinter/
2. https://pythonclassroomdiary.wordpress.com/python-
gui-projects/
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
In the same way you can use
other control

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
References
•https://www.tutorialspoint.com/
•https://effbot.org/tkinterbook/
•https://www.python-course.eu/

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Tags