Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 8 image grid with automatical image resize by screen resolution
Hello everybody,
i am very new in python and have a problem.
I use Python 3 on a Raspberry.
I wanted to have a little program what have an full width grid with 2 rows and 4 columns. The images inside every column i want to change by Gpio later. But i stuck at the grid with the images.
I wanted to show in every field an image. This works. I decided to show images inside label (i am not sure if this is correct way).
Now i have the problem that my window is full size. Depends on screen resolution. All my Images are 800 x 800. So i want to scale them down that i have in first row 4 images and in second row 4 images.
For example when i have 1024 screen width every image must be not more then 256px.
But exactly for this i dont find solution. I read a lot and tried several ways. But no success.
In the moment the images strech all and my grid is broken.

This is my actuall code:
# import tkinter module
from tkinter import *
from tkinter.ttk import *

# creating main tkinter window/toplevel
master = Tk()
# Turn of window frame
# getting screen's width & height in pixels
screen_width = master.winfo_screenwidth()
screen_height = master.winfo_screenheight()
# New Var with Geometry size
screen_resolution = str(screen_width)+'x'+str(screen_height)
# Set size for Window

# Grid settings
grid.rowconfigure(master, 0, weight=1)
grid.columnconfigure(master, 0, weight=1)

# Load images
# Row 1 Standard 
imgr1 = PhotoImage(file=r"images\close1.png")
imgr1s = imgr1.subsample(1, 1)
imgr1_1 = PhotoImage(file=r"images\close2.png")
imgr1_1s = imgr1_1.subsample(1, 1)
imgr1_2 = PhotoImage(file=r"images\close3.png")
imgr1_2s = imgr1_2.subsample(1, 1)
imgr1_3 = PhotoImage(file=r"images\close4.png")
imgr1_3s = imgr1_3.subsample(1, 1)

# Row 2 Standard 
imgr2 = PhotoImage(file=r"images\close5.png")
imgr2s = imgr2.subsample(2, 2)
imgr2_1 = PhotoImage(file=r"images\close6.png")
imgr2_1s = imgr2_1.subsample(2, 2)
imgr2_2 = PhotoImage(file=r"images\close7.png")
imgr2_2s = imgr2_2.subsample(2, 2)
imgr2_3 = PhotoImage(file=r"images\close8.png")
imgr2_3s = imgr2_3.subsample(2, 2)

# Row 1
# Set images in Row 1 by Label
Label(master, image=imgr1s).grid(row=0, column=0, pady=1, sticky=N+S+E+W)
Label(master, image=imgr1_1s).grid(row=0, column=1, pady=1, sticky=N+S+E+W)
Label(master, image=imgr1_2s).grid(row=0, column=2, pady=1, sticky=N+S+E+W)
Label(master, image=imgr1_3s).grid(row=0, column=3, pady=1, sticky=N+S+E+W)

# Row 2
# Set images in Row 2 by Label
Label(master, image=imgr2).grid(row=1, column=0, pady=1, sticky=N+S+E+W)
Label(master, image=imgr2_1).grid(row=1, column=1, pady=1, sticky=N+S+E+W)
Label(master, image=imgr2_2).grid(row=1, column=2, pady=1, sticky=N+S+E+W)
Label(master, image=imgr2_3).grid(row=1, column=3, pady=1, sticky=N+S+E+W)

# infinite loop which can be terminated  
# by keyboard or mouse interrupt 

Hopefully somebody can bring me to right way.
Thx for help
I have seen this done using the PIL module, it has a function to resize called thumbnail.
Instead of label they used a canvas object create_image. below is an function from a class I'm using to preview an icon image in my script hope it will point you in the right direction:
def change_icon(self, size=(40,40)): # size of the icon 40x40
        from PIL.ImageTk import PhotoImage, Image # 2 PIL objects
        self.can.delete('object')                 # removes exiting object from canvas
        pic_num= self.combo3.current()            # retrieves the index number drop down
        pic=[pic_num]                 # gets photo from list
        img_dir= r'../PIL/images/'
        img_obj= + pic)
        img_obj.thumbnail(size, Image.ANTIALIAS) self.img = PhotoImage(img_obj) #to prevent garbage collect
        icon= self.can.create_image(100,40,image=self.img,
        self.parent.update()                                 # self.parent is my Tk()
@AlexanderO Hey it would be nice if you folks using Tkinter would put that in your subject like others have -- it helps us non-Tkinters ignore something we cannot weigh in on -- thanks for your cooperation
silly American kah-nig-hit

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PySimpleGUI] error trying to resize Text element skratt 3 137 Dec-10-2019, 06:05 PM
Last Post: FullOfHelp
  [Tkinter] Image editor using Tkinter and PIL Raulica 2 101 Dec-03-2019, 10:10 PM
Last Post: joe_momma
  Unable to put background image on Tkinter Frame jenkins43 2 139 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  Zooming a tkinter image GalaxyCoyote 2 176 Nov-09-2019, 04:36 PM
Last Post: GalaxyCoyote
  [Tkinter] Display Selected Image from Directory and Resize it EchoLi 0 227 Oct-02-2019, 06:54 PM
Last Post: EchoLi
  Refresh image in label after every 1s using simple function jenkins43 1 356 Jul-28-2019, 02:49 PM
Last Post: Larz60+
  Button with Image Icon Friend 2 316 Jul-25-2019, 09:39 AM
Last Post: Friend
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 4,259 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Tkinter] Image does not show in treeview. KevinBrown 3 597 May-05-2019, 11:47 PM
Last Post: KevinBrown
  [Tkinter] Image with pillow on macos not showing rhoekstra 3 370 May-04-2019, 11:48 PM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)