Skip to content
Snippets Groups Projects
Commit 76d78d44 authored by Bui2.Huan@live.uwe.ac.uk's avatar Bui2.Huan@live.uwe.ac.uk
Browse files

Upload New File

parent 34b54fdb
Branches
No related tags found
No related merge requests found
%% 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()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment