0% found this document useful (0 votes)
6 views

SDF-Week_17

The document outlines key concepts in software development fundamentals, specifically focusing on code documentation and the use of the tkinter library for creating graphical user interfaces (GUIs) in Python. It provides guidelines for documenting Python code, introduces various tkinter widgets, and demonstrates how to create a window and manage layouts with the .grid() method. An activity is also included, prompting users to create a GUI for collecting user information.
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)
6 views

SDF-Week_17

The document outlines key concepts in software development fundamentals, specifically focusing on code documentation and the use of the tkinter library for creating graphical user interfaces (GUIs) in Python. It provides guidelines for documenting Python code, introduces various tkinter widgets, and demonstrates how to create a window and manage layouts with the .grid() method. An activity is also included, prompting users to create a GUI for collecting user information.
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/ 7

17/12/2024

Software Development Fundamentals


Week 17-1

Dr. Loubna Mekouar

Note:
• Some of the slides of the course are based on the material provided
by the College of Technological Information, Zayed University, UAE.
• The copyrighted material belongs to their respective owners.

1
17/12/2024

Code Documentation
- The Python code must contain proper documentation.
- Suggested header documentations are provided for programs and functions.
Program Header Suggestion:
# Program/Class Name:
# Author(s):
# Date of Creation:
Function Header Suggestion:
# Function Description:
# Parameters:
# Return Type:
- All programs and all functions must contain sufficient documentation to explain
their intent and use.

• Simple is better than complex: When choosing between a simple and a complex
solution, always opt for the simple one if both work effectively. Simpler code is
easier to maintain, understand, and extend.

Graphical User Interface


GUI with Python - tkinter

2
17/12/2024

The tkinter Library


• Python provides predefined classes to create Graphical User Interfaces
(GUI)

• The tkinter library, is the only GUI framework that is built into the Python
standard library

• While it is one of the earliest frameworks, tkinter is lightweight and


relatively easy to use.

• All functionalities are available by importing the tkinter library

import tkinter as tk

Window
• Create a window using import tkinter as tk
tkinter
# Create a window
window = tk.Tk()
• The module tkinter has
class Tk # Keeps focus on window till closed
window.mainloop()
• Standard window will
depend on the OS used

3
17/12/2024

Widgets
• Widgets are GUI elements inside a window
• Each widget in tkinter is defined by a class.

• Here are some of the widgets available:

Label A widget used to display text on the screen


Button A button that can contain text and can perform an action when clicked
Entry A text entry widget that allows only a single line of text
Text A text entry widget that allows multiline text entry

Label Widget
import tkinter as tk

# Create a window
window = tk.Tk()

# Create a label widget for the window


greeting = tk.Label(text=“Python") white
d color to
n
# Add the label to the window frame regrou lack
nge the fo color to b
Ch a oun d
greeting.pack() and b
ackgr

# Keeps focus on window till closed


window.mainloop()
8

4
17/12/2024

Button Widget
import tkinter as tk

# Create a window
window = tk.Tk()

# Create a label widget for the window


greeting = tk.Label(text="Python", fg="white", bg="black", width=20, height=10)

# Create a button widget for the window


button = tk.Button(text="Click me!", fg="yellow", bg="blue", width=10, height=2)

# Add the label and button to the window frame


greeting.pack()
button.pack()

# Keeps focus on window till closed


window.mainloop()

Entry Widget (Small Text Box)


import tkinter as tk

# Create a window
window = tk.Tk()

# Create Label and Entry


label = tk.Label(text="Name")
nameBox = tk.Entry()

# Create Button to get name in entry box


def getName():
print(nameBox.get())
btnName = tk.Button(text="Print Name", command=getName)
ox.
ntry b rks.
# Pack all widgets to the window clear the E it wo
to ow
label.pack() her button , find out h
t )
d ano elete(
nameBox.pack() • Ad ameBox.d
e n
btnName.pack() • Us

window.mainloop() 10

10

5
17/12/2024

Text Widget
import tkinter as tk

# Create a window
window = tk.Tk()

# Create Label and Entry


label = tk.Label(text="Type Letter")
txtBox = tk.Text()
ox.
# Pack all widgets to the window r t he Text b s.
clea work
label.pack() button to out how it
ther ind
d ano te(), f
txtBox.pack() • Ad xtBox.dele
et
• Us
window.mainloop()

11

11

Geometry Manager .grid()


• Geometry manager is used import tkinter as tk
to manage layout inside a
window # Create a Window
window = tk.Tk()

• Most commonly used # Creates a 3 × 3 grid layout


Geometry Manager is the # Insert Label widgets into grid
.grid() for i in range(3):
for j in range(3):
label = tk.Label(text=f"Row {i}\nColumn {j}")
• The .grid() splits a window label.grid(row=i,column=j)
into rows and columns. window.mainloop()

• Both row and column


indices start at 0,
• Row 1 and Column 2 tells
.grid() to place a widget in
the third column of the
second row.

12

12

6
17/12/2024

Activity
• Create a window, to ask the user for his Name, CIN, phone number,
and City.
• When the user clicks the “Save” button, clear the fields and print all
the information on the screen and to a text file.

13

13

In Summary
• GUI in Python

• Creating Window

• Adding Widgets

• Manage layout with .grid()

14

14

You might also like