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
4e6ed093
Commit
4e6ed093
authored
2 weeks ago
by
Bui2.Huan@live.uwe.ac.uk
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
76d78d44
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Week 8/activity4.ipynb
+241
-0
241 additions, 0 deletions
Week 8/activity4.ipynb
with
241 additions
and
0 deletions
Week 8/activity4.ipynb
0 → 100644
+
241
−
0
View file @
4e6ed093
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4a"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk, ttk\n",
"\n",
"# Create main window\n",
"wndw = Tk()\n",
"wndw.title(\"Search Example\")\n",
"wndw.geometry(\"300x150\") # Set window size\n",
"\n",
"# Create a frame\n",
"frm = ttk.Frame(wndw, padding=10)\n",
"frm.grid()\n",
"\n",
"# Add a text field (Entry widget)\n",
"entry = ttk.Entry(frm, width=20)\n",
"entry.grid(column=0, row=0, padx=5, pady=5)\n",
"\n",
"# Add a button (doesn't do anything yet)\n",
"ttk.Button(frm, text=\"SEARCH\").grid(column=1, row=0, padx=5, pady=5)\n",
"\n",
"# Start the event loop\n",
"wndw.mainloop()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4b"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk, messagebox, ttk\n",
"\n",
"# Function to capture and display text\n",
"def show_input():\n",
" user_input = entry.get() # Get text from entry field\n",
" messagebox.showinfo(\"Your Input\", f\"You entered: {user_input}\")\n",
"\n",
"# Create main window\n",
"wndw = Tk()\n",
"wndw.title(\"Search Example\")\n",
"wndw.geometry(\"300x150\")\n",
"\n",
"# Create a frame\n",
"frm = ttk.Frame(wndw, padding=10)\n",
"frm.grid()\n",
"\n",
"# Add a text field\n",
"entry = ttk.Entry(frm, width=20)\n",
"entry.grid(column=0, row=0, padx=5, pady=5)\n",
"\n",
"# Add a button that captures input\n",
"ttk.Button(frm, text=\"SEARCH\", command=show_input).grid(column=1, row=0, padx=5, pady=5)\n",
"\n",
"# Start the event loop\n",
"wndw.mainloop()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4c. improve 4b\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk, messagebox, ttk\n",
"\n",
"# Function to capture and display text\n",
"def show_input():\n",
" user_input = entry.get()\n",
" messagebox.showinfo(\"Your Input\", f\"You entered: {user_input}\")\n",
"\n",
"# Create main window\n",
"wndw = Tk()\n",
"wndw.title(\"Enhanced GUI\")\n",
"wndw.geometry(\"400x200\")\n",
"\n",
"# Create main frame\n",
"main_frame = ttk.Frame(wndw, padding=20)\n",
"main_frame.pack(expand=True)\n",
"\n",
"# Add label\n",
"ttk.Label(main_frame, text=\"Enter something and click SEARCH:\").pack(pady=5)\n",
"\n",
"# Add text entry\n",
"entry = ttk.Entry(main_frame, width=30)\n",
"entry.pack(pady=5)\n",
"\n",
"# Add button\n",
"ttk.Button(main_frame, text=\"SEARCH\", command=show_input).pack(pady=5)\n",
"\n",
"# Start the event loop\n",
"wndw.mainloop()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4d, e"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk, messagebox, ttk\n",
"\n",
"# Function to capture username and password\n",
"def login():\n",
" username = username_entry.get()\n",
" password = password_entry.get()\n",
" messagebox.showinfo(\"Login Info\", f\"Username: {username}\\nPassword: {password}\")\n",
"\n",
"# Create main window\n",
"wndw = Tk()\n",
"wndw.title(\"Login Form\")\n",
"wndw.geometry(\"300x200\")\n",
"\n",
"# Create frame\n",
"frm = ttk.Frame(wndw, padding=20)\n",
"frm.pack(expand=True)\n",
"\n",
"# Username field\n",
"ttk.Label(frm, text=\"Username:\").pack(pady=5)\n",
"username_entry = ttk.Entry(frm, width=25)\n",
"username_entry.pack(pady=5)\n",
"\n",
"# Password field (hide input with \"show='*'\")\n",
"ttk.Label(frm, text=\"Password:\").pack(pady=5)\n",
"password_entry = ttk.Entry(frm, width=25, show='*')\n",
"password_entry.pack(pady=5)\n",
"\n",
"# Login button\n",
"ttk.Button(frm, text=\"LOGIN\", command=login).pack(pady=10)\n",
"\n",
"# Start the event loop\n",
"wndw.mainloop()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4f"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from tkinter import Tk, messagebox, ttk\n",
"\n",
"class LoginApp:\n",
" def __init__(self, root):\n",
" self.root = root\n",
" self.root.title(\"Login Form\")\n",
" self.root.geometry(\"300x200\")\n",
"\n",
" # Create frame\n",
" frm = ttk.Frame(root, padding=20)\n",
" frm.pack(expand=True)\n",
"\n",
" # Username field\n",
" ttk.Label(frm, text=\"Username:\").pack(pady=5)\n",
" self.username_entry = ttk.Entry(frm, width=25)\n",
" self.username_entry.pack(pady=5)\n",
"\n",
" # Password field\n",
" ttk.Label(frm, text=\"Password:\").pack(pady=5)\n",
" self.password_entry = ttk.Entry(frm, width=25, show='*')\n",
" self.password_entry.pack(pady=5)\n",
"\n",
" # Login button\n",
" ttk.Button(frm, text=\"LOGIN\", command=self.login).pack(pady=10)\n",
"\n",
" # Login function\n",
" def login(self):\n",
" username = self.username_entry.get()\n",
" password = self.password_entry.get()\n",
" messagebox.showinfo(\"Login Info\", f\"Username: {username}\\nPassword: {password}\")\n",
"\n",
"# Create main window and run the app\n",
"if __name__ == \"__main__\":\n",
" root = Tk()\n",
" app = LoginApp(root)\n",
" root.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:
4a
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
,
ttk
# Create main window
wndw
=
Tk
()
wndw
.
title
(
"
Search Example
"
)
wndw
.
geometry
(
"
300x150
"
)
# Set window size
# Create a frame
frm
=
ttk
.
Frame
(
wndw
,
padding
=
10
)
frm
.
grid
()
# Add a text field (Entry widget)
entry
=
ttk
.
Entry
(
frm
,
width
=
20
)
entry
.
grid
(
column
=
0
,
row
=
0
,
padx
=
5
,
pady
=
5
)
# Add a button (doesn't do anything yet)
ttk
.
Button
(
frm
,
text
=
"
SEARCH
"
).
grid
(
column
=
1
,
row
=
0
,
padx
=
5
,
pady
=
5
)
# Start the event loop
wndw
.
mainloop
()
```
%% Cell type:markdown id: tags:
4b
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
,
messagebox
,
ttk
# Function to capture and display text
def
show_input
():
user_input
=
entry
.
get
()
# Get text from entry field
messagebox
.
showinfo
(
"
Your Input
"
,
f
"
You entered:
{
user_input
}
"
)
# Create main window
wndw
=
Tk
()
wndw
.
title
(
"
Search Example
"
)
wndw
.
geometry
(
"
300x150
"
)
# Create a frame
frm
=
ttk
.
Frame
(
wndw
,
padding
=
10
)
frm
.
grid
()
# Add a text field
entry
=
ttk
.
Entry
(
frm
,
width
=
20
)
entry
.
grid
(
column
=
0
,
row
=
0
,
padx
=
5
,
pady
=
5
)
# Add a button that captures input
ttk
.
Button
(
frm
,
text
=
"
SEARCH
"
,
command
=
show_input
).
grid
(
column
=
1
,
row
=
0
,
padx
=
5
,
pady
=
5
)
# Start the event loop
wndw
.
mainloop
()
```
%% Cell type:markdown id: tags:
4c. improve 4b
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
,
messagebox
,
ttk
# Function to capture and display text
def
show_input
():
user_input
=
entry
.
get
()
messagebox
.
showinfo
(
"
Your Input
"
,
f
"
You entered:
{
user_input
}
"
)
# Create main window
wndw
=
Tk
()
wndw
.
title
(
"
Enhanced GUI
"
)
wndw
.
geometry
(
"
400x200
"
)
# Create main frame
main_frame
=
ttk
.
Frame
(
wndw
,
padding
=
20
)
main_frame
.
pack
(
expand
=
True
)
# Add label
ttk
.
Label
(
main_frame
,
text
=
"
Enter something and click SEARCH:
"
).
pack
(
pady
=
5
)
# Add text entry
entry
=
ttk
.
Entry
(
main_frame
,
width
=
30
)
entry
.
pack
(
pady
=
5
)
# Add button
ttk
.
Button
(
main_frame
,
text
=
"
SEARCH
"
,
command
=
show_input
).
pack
(
pady
=
5
)
# Start the event loop
wndw
.
mainloop
()
```
%% Cell type:markdown id: tags:
4d, e
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
,
messagebox
,
ttk
# Function to capture username and password
def
login
():
username
=
username_entry
.
get
()
password
=
password_entry
.
get
()
messagebox
.
showinfo
(
"
Login Info
"
,
f
"
Username:
{
username
}
\n
Password:
{
password
}
"
)
# Create main window
wndw
=
Tk
()
wndw
.
title
(
"
Login Form
"
)
wndw
.
geometry
(
"
300x200
"
)
# Create frame
frm
=
ttk
.
Frame
(
wndw
,
padding
=
20
)
frm
.
pack
(
expand
=
True
)
# Username field
ttk
.
Label
(
frm
,
text
=
"
Username:
"
).
pack
(
pady
=
5
)
username_entry
=
ttk
.
Entry
(
frm
,
width
=
25
)
username_entry
.
pack
(
pady
=
5
)
# Password field (hide input with "show='*'")
ttk
.
Label
(
frm
,
text
=
"
Password:
"
).
pack
(
pady
=
5
)
password_entry
=
ttk
.
Entry
(
frm
,
width
=
25
,
show
=
'
*
'
)
password_entry
.
pack
(
pady
=
5
)
# Login button
ttk
.
Button
(
frm
,
text
=
"
LOGIN
"
,
command
=
login
).
pack
(
pady
=
10
)
# Start the event loop
wndw
.
mainloop
()
```
%% Cell type:markdown id: tags:
4f
%% Cell type:code id: tags:
```
python
from
tkinter
import
Tk
,
messagebox
,
ttk
class
LoginApp
:
def
__init__
(
self
,
root
):
self
.
root
=
root
self
.
root
.
title
(
"
Login Form
"
)
self
.
root
.
geometry
(
"
300x200
"
)
# Create frame
frm
=
ttk
.
Frame
(
root
,
padding
=
20
)
frm
.
pack
(
expand
=
True
)
# Username field
ttk
.
Label
(
frm
,
text
=
"
Username:
"
).
pack
(
pady
=
5
)
self
.
username_entry
=
ttk
.
Entry
(
frm
,
width
=
25
)
self
.
username_entry
.
pack
(
pady
=
5
)
# Password field
ttk
.
Label
(
frm
,
text
=
"
Password:
"
).
pack
(
pady
=
5
)
self
.
password_entry
=
ttk
.
Entry
(
frm
,
width
=
25
,
show
=
'
*
'
)
self
.
password_entry
.
pack
(
pady
=
5
)
# Login button
ttk
.
Button
(
frm
,
text
=
"
LOGIN
"
,
command
=
self
.
login
).
pack
(
pady
=
10
)
# Login function
def
login
(
self
):
username
=
self
.
username_entry
.
get
()
password
=
self
.
password_entry
.
get
()
messagebox
.
showinfo
(
"
Login Info
"
,
f
"
Username:
{
username
}
\n
Password:
{
password
}
"
)
# Create main window and run the app
if
__name__
==
"
__main__
"
:
root
=
Tk
()
app
=
LoginApp
(
root
)
root
.
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