diff --git a/week 8/activity3.ipynb b/week 8/activity3.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..eca4ce2c4c2c0cfa0d482045f3b3f2c121f6aca6 --- /dev/null +++ b/week 8/activity3.ipynb @@ -0,0 +1,191 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "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()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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()" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}