Posts: 61
Threads: 29
Joined: Oct 2023
i have the basic code that follows
window = Tk()
window.title("Reminder Application")
window.geometry("1079x719")
bg_pic = PhotoImage(file="christ_photo.png")
canvas = Canvas(window, width=835, height=560)
canvas.grid(row=0, column=1, columnspan=10)
canvas.create_image(400, 337, image=bg_pic) im trying to display a background image. it appears however that the picture in the interface i zoomed in.. i first attach a picture of the GUI of the app
this is my GUI window
and here is the original picture
for some reason when i display the picture to the interface it zooms in. Ive trying tinkering with the width and height params but i cant get it to zoom out so the whole picture can be in the interface
Posts: 1,144
Threads: 114
Joined: Sep 2019
Jun-24-2024, 04:12 PM
(This post was last modified: Jun-24-2024, 04:12 PM by menator01.)
You may be better off using a label to display the image
The images you posted are not showing
Posts: 61
Threads: 29
Joined: Oct 2023
(Jun-24-2024, 04:12 PM)menator01 Wrote: You may be better off using a label to display the image
The images you posted are not showing
I tried using the label but i could get it to align in the middle and it opens the pic in its initial size whereas i want to crop it and have it in the centre of the app.. here are the two pictures i attached before
first the interface picture with the canvas.create_image option
https://ibb.co/M7RxQgR
and following that the original picture
https://ibb.co/WB0x8B9
Posts: 1,144
Threads: 114
Joined: Sep 2019
Jun-24-2024, 05:15 PM
(This post was last modified: Jun-24-2024, 05:15 PM by menator01.)
Are you trying to get something like this?
import tkinter as tk
from PIL import Image, ImageTk
import os
# Setting a path to executing script
path = os.path.realpath(os.path.dirname(__file__))
# Using pillow to open and resize image
image = Image.open(f'{path}/christ-photo.jpg')
# Get width and height of image
width, height = image.size
# Number to divide by
num = 6
# Set the size
size = width//num, height//num
image = image.resize(size)
root = tk.Tk()
# Using geometry manager
root.geometry('800x600+250+250')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
# Using frame as a container
container = tk.Frame(root)
container.grid(column=0, row=0, sticky='news', padx=10, pady=10)
# Configuring container columns
for i in range(5):
container.grid_columnconfigure(i, weight=3)
# Setting image to display in tkinter
image = ImageTk.PhotoImage(image)
# creating a label and setting image
label = tk.Label(container, image=image)
label.grid(column=0, columnspan=5, row=0, sticky='news', pady=10)
# Ceating some test label.
for i in range(5):
label = tk.Label(container, text=f'label {i}')
label.grid(column=i, row=1, sticky='new')
# Setting columns using columnconfigure. Uniform ensures all columns are same size
label.grid_columnconfigure(i, weight=3, uniform='col')
root.mainloop()
Posts: 61
Threads: 29
Joined: Oct 2023
(Jun-24-2024, 05:15 PM)menator01 Wrote: Are you trying to get something like this?
import tkinter as tk
from PIL import Image, ImageTk
import os
# Setting a path to executing script
path = os.path.realpath(os.path.dirname(__file__))
# Using pillow to open and resize image
image = Image.open(f'{path}/christ-photo.jpg')
# Get width and height of image
width, height = image.size
# Number to divide by
num = 6
# Set the size
size = width//num, height//num
image = image.resize(size)
root = tk.Tk()
# Using geometry manager
root.geometry('800x600+250+250')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
# Using frame as a container
container = tk.Frame(root)
container.grid(column=0, row=0, sticky='news', padx=10, pady=10)
# Configuring container columns
for i in range(5):
container.grid_columnconfigure(i, weight=3)
# Setting image to display in tkinter
image = ImageTk.PhotoImage(image)
# creating a label and setting image
label = tk.Label(container, image=image)
label.grid(column=0, columnspan=5, row=0, sticky='news', pady=10)
# Ceating some test label.
for i in range(5):
label = tk.Label(container, text=f'label {i}')
label.grid(column=i, row=1, sticky='new')
# Setting columns using columnconfigure. Uniform ensures all columns are same size
label.grid_columnconfigure(i, weight=3, uniform='col')
root.mainloop()
thanks for your help. if anybody sees this post looking for a solution i ended up just resizing the picture using paint in order to better match the dimensions of the widgets
Posts: 38
Threads: 0
Joined: Jun 2024
Your image might be zoomed in because of theĀ CanvasĀ size. Try resizing the image to fit the canvas before displaying it. from tkinter import Tk, Canvas from PIL import Image, ImageTk window = Tk() window.title("Reminder Application") window.geometry("1079x719") #Load and resize the image image = Image.open("christ_photo.png").resize((835, 560), Image.ANTIALIAS) bg_pic = ImageTk.PhotoImage(image) # Create the canvas and add the resized image canvas = Canvas(window, width=835, height=560) canvas.grid(row=0, column=1, columnspan=10) canvas.create_image(0, 0, anchor='nw', image=bg_pic) window.mainloop() This should make the image fit the canvas. Hope this helps!
|