Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
Lab Advanced Software Development
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bui2.Huan@live.uwe.ac.uk
Lab Advanced Software Development
Commits
76d78d44
Commit
76d78d44
authored
2 months ago
by
Bui2.Huan@live.uwe.ac.uk
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
34b54fdb
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Week 8/activity3.ipynb
+240
-0
240 additions, 0 deletions
Week 8/activity3.ipynb
with
240 additions
and
0 deletions
Week 8/activity3.ipynb
0 → 100644
+
240
−
0
View file @
76d78d44
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3a"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk\n",
"\n",
"# Create main window\n",
"wndw = Tk()\n",
"wndw.title(\"My Empty Window\")\n",
"\n",
"# Set window size\n",
"wndw.geometry(\"300x200\")\n",
"\n",
"# Start the main event loop\n",
"wndw.mainloop()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3b"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk\n",
"from tkinter import ttk\n",
"\n",
"# Create main window\n",
"wndw = Tk()\n",
"wndw.title(\"My Simple GUI\")\n",
"wndw.geometry(\"300x200\")\n",
"\n",
"# Create frame\n",
"frm = ttk.Frame(wndw, padding=10)\n",
"frm.grid()\n",
"\n",
"# Add label\n",
"ttk.Label(frm, text=\"Welcome to Tkinter!\").grid(column=0, row=0)\n",
"\n",
"# Add button\n",
"ttk.Button(frm, text=\"Hello UWE\").grid(column=0, row=1)\n",
"\n",
"# Start the main event loop\n",
"wndw.mainloop()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3c"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk, messagebox\n",
"from tkinter import ttk\n",
"\n",
"# Create main window\n",
"wndw = Tk()\n",
"wndw.title(\"Interactive GUI\")\n",
"wndw.geometry(\"300x200\")\n",
"\n",
"# Function to show message\n",
"def show_message():\n",
" messagebox.showinfo(\"Greeting\", \"Hello UWE!\")\n",
"\n",
"# Create frame\n",
"frm = ttk.Frame(wndw, padding=10)\n",
"frm.grid()\n",
"\n",
"# Add label\n",
"ttk.Label(frm, text=\"Click the button below:\").grid(column=0, row=0)\n",
"\n",
"# Add button and link function\n",
"ttk.Button(frm, text=\"Hello UWE\", command=show_message).grid(column=0, row=1)\n",
"\n",
"# Start the main event loop\n",
"wndw.mainloop()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3d"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk, Canvas\n",
"\n",
"# Create main window\n",
"wndw = Tk()\n",
"wndw.title(\"Canvas Example\")\n",
"wndw.geometry(\"300x200\")\n",
"\n",
"# Create canvas\n",
"cnvs = Canvas(wndw, width=200, height=150, bg=\"red\")\n",
"cnvs.pack(pady=20)\n",
"\n",
"# Start the main event loop\n",
"wndw.mainloop()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.e, f"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk, Canvas, PhotoImage\n",
"from PIL import Image, ImageTk\n",
"\n",
"# Create main window\n",
"wndw = Tk()\n",
"wndw.title(\"Resized Image on Canvas\")\n",
"wndw.geometry(\"300x200\")\n",
"\n",
"# Open and resize the image\n",
"original_image = Image.open(\"UWE Bristol.png\") # Replace with your image path\n",
"resized_image = original_image.resize((200, 150)) # Match canvas size\n",
"\n",
"# Convert the resized image to PhotoImage\n",
"uwe_logo = ImageTk.PhotoImage(resized_image)\n",
"\n",
"# Create canvas\n",
"cnvs = Canvas(wndw, width=200, height=150)\n",
"cnvs.pack()\n",
"\n",
"# Add image to canvas\n",
"cnvs.create_image(100, 75, image=uwe_logo) # Center of canvas\n",
"\n",
"# Start the main event loop\n",
"wndw.mainloop()\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk, Canvas, messagebox, ttk\n",
"from PIL import Image, ImageTk # For image resizing\n",
"\n",
"# Function to show a message when button is clicked\n",
"def show_message():\n",
" messagebox.showinfo(\"Greeting\", \"Hello UWE!\")\n",
"\n",
"# Create main window\n",
"wndw = Tk()\n",
"wndw.title(\"Complete GUI Example\")\n",
"wndw.geometry(\"400x300\") # Adjusted size for better layout\n",
"\n",
"# Create a frame for better organization\n",
"frm = ttk.Frame(wndw, padding=10)\n",
"frm.grid()\n",
"\n",
"# Add a label\n",
"ttk.Label(frm, text=\"Welcome to My Tkinter GUI!\").grid(column=0, row=0, columnspan=2)\n",
"\n",
"# Add a button that shows a message when clicked\n",
"ttk.Button(frm, text=\"Hello UWE\", command=show_message).grid(column=0, row=1)\n",
"\n",
"# Create canvas\n",
"canvas_width = 200\n",
"canvas_height = 150\n",
"cnvs = Canvas(frm, width=canvas_width, height=canvas_height, bg=\"red\")\n",
"cnvs.grid(column=1, row=1, padx=10, pady=10) # Position next to button\n",
"\n",
"# Load and resize image (make sure \"uwe_logo.png\" exists in the same directory)\n",
"\n",
"original_image = Image.open(\"UWE Bristol.png\") # Replace with your image file\n",
"resized_image = original_image.resize((canvas_width, canvas_height)) # Resize to fit canvas\n",
"uwe_logo = ImageTk.PhotoImage(resized_image)\n",
"\n",
"# Add image to canvas\n",
"cnvs.create_image(canvas_width // 2, canvas_height // 2, image=uwe_logo)\n",
"\n",
"\n",
"# Start the main event loop\n",
"wndw.mainloop()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
3a
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
# Create main window
wndw
=
Tk
()
wndw
.
title
(
"
My Empty Window
"
)
# Set window size
wndw
.
geometry
(
"
300x200
"
)
# Start the main event loop
wndw
.
mainloop
()
```
%% Cell type:markdown id: tags:
3b
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
from
tkinter
import
ttk
# Create main window
wndw
=
Tk
()
wndw
.
title
(
"
My Simple GUI
"
)
wndw
.
geometry
(
"
300x200
"
)
# Create frame
frm
=
ttk
.
Frame
(
wndw
,
padding
=
10
)
frm
.
grid
()
# Add label
ttk
.
Label
(
frm
,
text
=
"
Welcome to Tkinter!
"
).
grid
(
column
=
0
,
row
=
0
)
# Add button
ttk
.
Button
(
frm
,
text
=
"
Hello UWE
"
).
grid
(
column
=
0
,
row
=
1
)
# Start the main event loop
wndw
.
mainloop
()
```
%% Cell type:markdown id: tags:
3c
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
,
messagebox
from
tkinter
import
ttk
# Create main window
wndw
=
Tk
()
wndw
.
title
(
"
Interactive GUI
"
)
wndw
.
geometry
(
"
300x200
"
)
# Function to show message
def
show_message
():
messagebox
.
showinfo
(
"
Greeting
"
,
"
Hello UWE!
"
)
# Create frame
frm
=
ttk
.
Frame
(
wndw
,
padding
=
10
)
frm
.
grid
()
# Add label
ttk
.
Label
(
frm
,
text
=
"
Click the button below:
"
).
grid
(
column
=
0
,
row
=
0
)
# Add button and link function
ttk
.
Button
(
frm
,
text
=
"
Hello UWE
"
,
command
=
show_message
).
grid
(
column
=
0
,
row
=
1
)
# Start the main event loop
wndw
.
mainloop
()
```
%% Cell type:markdown id: tags:
3d
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
,
Canvas
# Create main window
wndw
=
Tk
()
wndw
.
title
(
"
Canvas Example
"
)
wndw
.
geometry
(
"
300x200
"
)
# Create canvas
cnvs
=
Canvas
(
wndw
,
width
=
200
,
height
=
150
,
bg
=
"
red
"
)
cnvs
.
pack
(
pady
=
20
)
# Start the main event loop
wndw
.
mainloop
()
```
%% Cell type:markdown id: tags:
3.
e, f
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
,
Canvas
,
PhotoImage
from
PIL
import
Image
,
ImageTk
# Create main window
wndw
=
Tk
()
wndw
.
title
(
"
Resized Image on Canvas
"
)
wndw
.
geometry
(
"
300x200
"
)
# Open and resize the image
original_image
=
Image
.
open
(
"
UWE Bristol.png
"
)
# Replace with your image path
resized_image
=
original_image
.
resize
((
200
,
150
))
# Match canvas size
# Convert the resized image to PhotoImage
uwe_logo
=
ImageTk
.
PhotoImage
(
resized_image
)
# Create canvas
cnvs
=
Canvas
(
wndw
,
width
=
200
,
height
=
150
)
cnvs
.
pack
()
# Add image to canvas
cnvs
.
create_image
(
100
,
75
,
image
=
uwe_logo
)
# Center of canvas
# Start the main event loop
wndw
.
mainloop
()
```
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
,
Canvas
,
messagebox
,
ttk
from
PIL
import
Image
,
ImageTk
# For image resizing
# Function to show a message when button is clicked
def
show_message
():
messagebox
.
showinfo
(
"
Greeting
"
,
"
Hello UWE!
"
)
# Create main window
wndw
=
Tk
()
wndw
.
title
(
"
Complete GUI Example
"
)
wndw
.
geometry
(
"
400x300
"
)
# Adjusted size for better layout
# Create a frame for better organization
frm
=
ttk
.
Frame
(
wndw
,
padding
=
10
)
frm
.
grid
()
# Add a label
ttk
.
Label
(
frm
,
text
=
"
Welcome to My Tkinter GUI!
"
).
grid
(
column
=
0
,
row
=
0
,
columnspan
=
2
)
# Add a button that shows a message when clicked
ttk
.
Button
(
frm
,
text
=
"
Hello UWE
"
,
command
=
show_message
).
grid
(
column
=
0
,
row
=
1
)
# Create canvas
canvas_width
=
200
canvas_height
=
150
cnvs
=
Canvas
(
frm
,
width
=
canvas_width
,
height
=
canvas_height
,
bg
=
"
red
"
)
cnvs
.
grid
(
column
=
1
,
row
=
1
,
padx
=
10
,
pady
=
10
)
# Position next to button
# Load and resize image (make sure "uwe_logo.png" exists in the same directory)
original_image
=
Image
.
open
(
"
UWE Bristol.png
"
)
# Replace with your image file
resized_image
=
original_image
.
resize
((
canvas_width
,
canvas_height
))
# Resize to fit canvas
uwe_logo
=
ImageTk
.
PhotoImage
(
resized_image
)
# Add image to canvas
cnvs
.
create_image
(
canvas_width
//
2
,
canvas_height
//
2
,
image
=
uwe_logo
)
# Start the main event loop
wndw
.
mainloop
()
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment