Home > OS >  Trying to resize image on 2nd window of Tkinter GUI
Trying to resize image on 2nd window of Tkinter GUI

Time:10-13

This code will open a main window with an image, then another window with the same image. Is there any way to resize the image to be smaller? (go to # !>>> IMAGE 2 (2nd window))

Here is the code:

from tkinter import ttk
from tkinter import *

root = Tk()
root.title('4 Dry Out')
# IMAGE 1 (1st window)
img=PhotoImage(file='4 Dry Out Logo.png')
Label(root,image=img).pack()
# window format
root.geometry("275x75")
root['bg']='blue'

class MainWin:
    # main window frame
    def __init__(self, master):
        mainFrame = Frame(master)
        mainFrame.pack()
        # main window title / button 
        self.titleLabel = Label(master, text="4 Dry Out e-Rental", bg="blue", fg="white", font=("Arial Black", 20))
        self.titleLabel.pack()
        self.Btn = Button(master, text="Water Damage Equipment", command=self.MenuWin, bg="navy", fg="white", font=("Roboto")).pack()
       
    # button: new window
    def MenuWin(self):
        self.record = Menu()
        self.record.win.mainloop()
        
class Menu:
    # new window frame 
    def __init__(self):
        self.win = Toplevel()
        self.frameFit = Frame(self.win)
        self.frameFit.pack()
        self.frameFit['bg']='blue'
    # !>>> IMAGE 2 (2nd window)
        photo = PhotoImage(file='4 Dry Out Logo.png')
        label = Label(self.win,image=photo)
        label.image = photo # reference!
        label.pack()
        # portal title 
        self.TitleLabel = Label(self.frameFit, text="e-Rental Portal", bg="blue", fg="white", font=("Arial Black",15)).pack()
        
# start / end             
winStart = MainWin(root)
root.mainloop()

CodePudding user response:

The error NameError: name 'photo' is not defined is coming from this line:

tktext_label.image = photo

Like the error says, you've never defined photo. I'm guessing you just copied this code from somewhere without understanding what the code is doing. In this case, the code you copied is trying to save a reference to an image that was created earlier. You either renamed the image or you change the name in this statement, causing the error. It has nothing to do with creating a second window.

What the code should be is similar to the following, though I've added some comments to show the three places that need to use the same name:

    img=PhotoImage(file='4 Dry Out Logo.png')
    ###
    Label(self.win,image=img).pack()
                         ###
    tktext_label.image = img
                         ###

Your problem was that you used photo in the last line when you should have used img.

The point of the code is that after creating the image, it saves a reference to the image by assigning it to tktext_label.image. The reason for saving the reference is explained in screenshot

CodePudding user response:

Well first of all, you are referencing photo which is not defined as a variable (unless it's in another file and you haven't imported it). That's where the error is coming from.

For resizing the image you will need the PIL package - pip install pillow

After this you can import it and use it this way:

from PIL import Image, ImageTk
img = (Image.open("4 Dry Out Logo.png"))
image_resize = img.resize((w, h), Image.ANTIALIAS)
final_image = imageTK.PhotoImage(image_resize)
  • Related