Skip to content
Snippets Groups Projects
Commit 3b1c7091 authored by Nguyen5.Trung@live.uwe.ac.uk's avatar Nguyen5.Trung@live.uwe.ac.uk
Browse files

Add week 8,9,10 to repository

parent 532eb170
Branches
No related tags found
No related merge requests found
from tkinter import *
from tkinter import ttk
wndw = Tk()
wndw.title("This is my first TK")
#grid
# frm = ttk.Frame(wndw, padding=100)
# frm.grid()
# ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
# ttk.Button(frm, text="Quit", command=wndw.destroy).grid(column=1,
# row=0)
# wndw.mainloop()
#pack
frm = ttk.Frame(wndw, padding=10)
frm.pack()
ttk.Label(frm, text="Hello World!").pack(side=LEFT, padx=0, pady=100)
ttk.Button(frm, text="Quit", command=wndw.destroy).pack(side=RIGHT, padx=5, pady=5)
wndw.mainloop()
#place
# frm = ttk.Frame(wndw, padding=10)
# frm.place(x=20, y=20) # Specify exact position
# label = ttk.Label(frm, text="Hello World!")
# label.place(x=10, y=10) # Manually set coordinates
# button = ttk.Button(frm, text="Quit", command=wndw.destroy)
# button.place(x=100, y=10) # Manually set coordinates
# wndw.geometry("300x150") # Set window size for better visibility
# wndw.mainloop()
\ No newline at end of file
week8/UWE Bristol.png

23.4 KiB

%% 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()
```
%% 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}\nPassword: {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}\nPassword: {password}")
# Create main window and run the app
if __name__ == "__main__":
root = Tk()
app = LoginApp(root)
root.mainloop()
```
from tkinter import Tk, messagebox, ttk
from datetime import datetime
class Person:
def __init__(self, first_name, surname, dob, gender):
self.first_name = first_name
self.surname = surname
self.dob = dob
self.gender = gender
def get_details(self):
return f"Name: {self.first_name} {self.surname}\nGender: {self.gender}"
def get_age(self):
try:
birth_date = datetime.strptime(self.dob, "%d/%m/%Y")
today = datetime.today()
age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
return age
except ValueError:
return "Invalid date format"
people = []
def register():
first_name = first_name_entry.get()
surname = surname_entry.get()
gender = gender_var.get()
dob = dob_entry.get()
if not first_name or not surname or not dob or gender == "Select":
messagebox.showwarning("Warning", "Please fill out all fields!")
return
person = Person(first_name, surname, dob, gender)
people.append(person)
messagebox.showinfo("Registration Successful", person.get_details())
def show_total_registrations():
messagebox.showinfo("Total Registrations", f"Total registered users: {len(people)}")
def get_age_of_user():
first_name = first_name_entry.get()
surname = surname_entry.get()
for person in people:
if person.first_name == first_name and person.surname == surname:
age = person.get_age()
messagebox.showinfo("User Age", f"{first_name} {surname} is {age} years old.")
return
messagebox.showerror("Error", "User not found.")
wndw = Tk()
wndw.title("User Registration")
wndw.geometry("400x300")
frm = ttk.Frame(wndw, padding=20)
frm.pack(expand=True)
ttk.Label(frm, text="First Name:").pack(pady=5)
first_name_entry = ttk.Entry(frm, width=30)
first_name_entry.pack(pady=5)
ttk.Label(frm, text="Surname:").pack(pady=5)
surname_entry = ttk.Entry(frm, width=30)
surname_entry.pack(pady=5)
ttk.Label(frm, text="Gender:").pack(pady=5)
gender_var = ttk.Combobox(frm, values=["Male", "Female", "Other"], width=27)
gender_var.pack(pady=5)
gender_var.set("Select")
ttk.Label(frm, text="Date of Birth (DD/MM/YYYY):").pack(pady=5)
dob_entry = ttk.Entry(frm, width=30)
dob_entry.pack(pady=5)
ttk.Button(frm, text="Register", command=register).pack(pady=5)
ttk.Button(frm, text="Total Registrations", command=show_total_registrations).pack(pady=5)
ttk.Button(frm, text="Get Age", command=get_age_of_user).pack(pady=5)
wndw.mainloop()
from tkinter import Tk, ttk
# Create main window
wndw = Tk()
wndw.title("Coursework GUI")
wndw.geometry("500x500")
# Create a Notebook (tabbed interface)
notebook = ttk.Notebook(wndw)
notebook.pack(expand=True, fill='both')
# Home tab
home_frame = ttk.Frame(notebook)
ttk.Label(home_frame, text="Welcome to the Coursework GUI!", font=("Helvetica", 14)).pack(pady=20)
notebook.add(home_frame, text="Home")
# Registration tab
registration_frame = ttk.Frame(notebook)
ttk.Label(registration_frame, text="User Registration", font=("Helvetica", 14)).pack(pady=20)
notebook.add(registration_frame, text="Registration")
# About tab
about_frame = ttk.Frame(notebook)
ttk.Label(about_frame, text="About this application", font=("Helvetica", 14)).pack(pady=20)
notebook.add(about_frame, text="About")
# Registration form elements
ttk.Label(registration_frame, text="First Name:").pack(pady=5)
first_name_entry = ttk.Entry(registration_frame, width=30)
first_name_entry.pack(pady=5)
ttk.Label(registration_frame, text="Surname:").pack(pady=5)
surname_entry = ttk.Entry(registration_frame, width=30)
surname_entry.pack(pady=5)
ttk.Label(registration_frame, text="Gender:").pack(pady=5)
gender_var = ttk.Combobox(registration_frame, values=["Male", "Female", "Other"], width=27)
gender_var.pack(pady=5)
gender_var.set("Select")
ttk.Label(registration_frame, text="Date of Birth (DD/MM/YYYY):").pack(pady=5)
dob_entry = ttk.Entry(registration_frame, width=30)
dob_entry.pack(pady=5)
# Register button
def register_user():
first_name = first_name_entry.get()
surname = surname_entry.get()
gender = gender_var.get()
dob = dob_entry.get()
if not first_name or not surname or gender == "Select" or not dob:
ttk.Label(registration_frame, text="Please fill out all fields!", foreground='red').pack(pady=5)
return
ttk.Label(registration_frame, text=f"Registered: {first_name} {surname}").pack(pady=5)
ttk.Button(registration_frame, text="Register", command=register_user).pack(pady=10)
# About content
ttk.Label(about_frame, text="This is a sample coursework GUI.\nBuilt using Python Tkinter.", justify='center').pack(pady=20)
# Start the Tkinter event loop
wndw.mainloop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment