A complete presentation on how we can code Graphical user inteface using Python
Size: 139.83 KB
Language: en
Added: Sep 04, 2024
Slides: 30 pages
Slide Content
GUI Development in Python
Introduction to GUI Programming
Designing and building graphical interfaces that allow
users to interact with software using visual elements.
●Widgets: Basic visual elements like buttons labels, text
boxes, and menus.
●Layout: Arranging widgets in a window or container.
●Events: User interactions like clicks, key presses, and
mouse movements.
●Event Handling: Writing code to respond events.
Introduction to GUI Programming
GUI Frameworks
1.Tkinter: Built-in python library for creating simple GUIs.
2.PyQt: Powerful framework for building complex GUIs
3.wxPython: Cross-platform framework for building GUIs.
4.JavaFX: Modern framework for building GUIs.
5.Swing : Older framework for building GUIs.
Basic Steps:
→ Choose a framework→ Design the interface
→ Write event handlers→ Assemble the GUI
→ Test and refine
Installing PyCharm
Windows :
→ Download PyCharm installer.
→ Run installer and follow
prompts.
→ Choose installation location
and components.
Introduction to GUI Programming on PyCharm
Step - 1 : Create a New project
Step - 2 : Install GUI library
Stap - 3 : Create a GUI window
Step - 4 : Add GUI widgets
Step - 5 : Run the GUI App
Introduction to Tkinter
Tkinter is a python’s de-facto standard GUI package.
Tkinter Widgets :
1.Label : Displays text or images.
2.Button : A Clickable button.
3.Entry : A text input field.
4.Text : A multi-line text box.
5.Frame: A container for other widgets.
Introduction to Tkinter
Basic Tkinter Concepts :
1.Root Window : The main application window.
2.Widgets : GUI elements like buttons, labels, etc.
3.Layout Managers : Arrange widgets in the window.
4.Events : User interaction like clicks,key presses, etc
5.Callbacks : Functions that handle events.
1.Use the iconbitmap() method for Windows :
``
root = (link unavailable)()
root.iconbitmap(“icon.ico”)
``
2.Use the iconphoto() method for macOS and Linux :
``
root = (link unavailable)()
icon = tk.PhotoImage(file=”icon.png”)
root.iconphoto(False, icon)
``
Layouts in Tkinter
Three layout managers to arrange widgets in window :
1.Pack :
→ Simple and easy to use.
→ widgets are added in a vertical or horizontal box.
→ It can fill, expand, side, padx, pady.
Introduction to Grid Layout
It's a Powerful tool for arranging widgets in a table-like structure.
●Rows and Columns : Divide window into rows and columns.
●Widgets : Place widgets in specific cells of the grid.
Grid Operations :
1.Row : Specify the row number
2.Column : Specify the column number.
3.Sticky : Align widget within cell
4.Padx and pady : Add horizontal and vertical padding around widget
5.Rowspan and columnspan : Make widget span multiple rows or columns
Integration using GUI Designing User Interface
Integrating GUI design with user interface design involves creating a
visually appealing and interactive layout for your application.
1.Plan your Layout
2.Choose a color scheme
3.Select fonts
4.Design Widgets
5.Use layout Managers
6.Add Spacing and padding
7.Implement navigation
8.Test and refine
Integration using GUI Events
It involves connecting widgets to actions and events.
1.Define event handlers: Write functions that responds to events.
2.Blind Events to widgets: Use methods like command or bind to
link event handlers to widgets.
3.Handle events: Perform actions when events occur.
Common GUI Events :
●Button clicks
●Key presses
●Mouse clicks
●Window close
Integration using GUI Functions
Connecting widgets to actions and events and using functions to
perform tasks.
1.Define functions : Perform tasks such as calculations, data
processing.
2.Create widget : Add buttons, labels, text entries etc.
3.Bind Functions to widget : Use methods like command or bind
to link functions to widget.
4.Handle events : Respond User Interface.
Common GUI Functions are Button commands, Calculation, Data
validation and file handling functions.
Drop down menu
Customizations of a drop down menu :
●Changing the option list
●Setting a default value using variable.set()
●Using variable.get() to retrieve the selected option
●Blinding a function to the menu selection using variable.trace()
EX:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
dropdown = ttk.Combobox(root, values=["Option 1",
"Option 2", "Option 3"])
dropdown.pack()
root.mainloop()
Toolbar
Customizations of a Toolbar menu :
●Changing the button text and commands
●Adding more buttons or widgets
●Using pack, grid, place to arrange widgets in the toolbar
●Configuring the toolbar appearance
EX:
import tkinter as tk
root = tk.Tk()
root.title("Simple GUI")
# Run the application
root.mainloop()
Status Bar
Display information about the current state of the application.
Functions of a status bar :
●tk.Label : Display text or image
●tk.Progressbar : display progress of operation
●tk.Button : Performs actions when clicked
EX: label = tk.Label(root, text="This is a simple widget")
label.pack()
status_bar = tk.Label(root, text="Status: Ready", anchor=tk.W)
status_bar.pack(side=tk.BOTTOM, fill=tk.X)
root.mainloop()
Graphical Box
A rectangular area in a graphical UI that can contain various elements
Such as..,
1.Text
2.Images
3.Shapes
4.Icons
5.Buttons
6.Other GUI elements
Types of Graphical Boxes :
●Frames
●Panels
●Window
●Dialog boxes
Graphical Shape - Canvas
It can draw and manipulate various shapes such as..,
1.1. Lines
2.Rectangles
3.Eclipses
4.Polygons
5.Circles
6.Arcs
7.Curves
Customize the shape by using..,
●fill
●outline
●width
●dash
Images in GUI
Add images by using the PhotoImage class for PNG,PPM and PGM
images or the ImageTk module for other formats like JPEG.