-1

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()
Lady
  • 21
  • 9

3 Answers3

1

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 this answer to the question Why does Tkinter image not show up if created in a function?

Also, is there any way to resize the image to be smaller?

A simple search of this site will answer that question.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • After martineau's answer I came to the same conclusion, and actually kind of understand the concept of it- hah. However while on the site for the Tkinter image bug, I found another method / the full version of what I was trying to do. Updated code above ^^ how would I be able to resize in this style/format? – Lady Oct 12 '21 at 23:08
1

I see Bryan Oakley has already posted an answer your question, but I'll supplement it with my own, which also fixes several other problems (some related to this) I noticed in your code and shows how to resize the image without using PIL using the subsample() method Bryan mentioned in a comment under your related question that was closed as a duplicate.

You can find some documentation on it, copy(), zoom(), as well as the other methods of the Photoimage class has by using Python's built-in help system from the Python console: i.e.

>>> import tkinter
>>> help(tkinter.PhotoImage)

It's also in the source code of course.

Here's the code in your question code with fixes:

from tkinter import ttk
from tkinter import *

#image_filename = '4 Dry Out Logo.png'
image_filename = '8-ball.png'  # I don't have your image.

root = Tk()
root.title('4 Dry Out')
# IMAGE 1 (1st window)
img = PhotoImage(file=image_filename)
Label(root,image=img).pack()
# window format
root.geometry("500x500")
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"))
        self.Btn.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 <<<
#        img = PhotoImage(file='4 Dry Out Logo.png')
        small_img = img.subsample(2)   # Smaller copy of global img size 50%
        Label(self.win, image=small_img).pack()
        self.lbl_image = small_img  # Save reference to local image object.
        # portal title
        self.TitleLabel = Label(self.frameFit, text="e-Rental Portal", bg="blue",
                                fg="white", font=("Arial Black", 15))
        self.TitleLabel.pack()

# start / end
winStart = MainWin(root)
root.mainloop()

I don't have your 4 Dry Out Logo.png logo image, but here's how things looked after clicking the button while running on my system using a substitute image .

screenshot

martineau
  • 119,623
  • 25
  • 170
  • 301
0

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)
Jack
  • 1
  • 2
  • I am running Linux on a Chromebook and for some reason it won't install pip or PIL, hence why I am going so far around to find another method to use. How would I go about defining [photo]? – Lady Oct 12 '21 at 20:42
  • You can try installing pip using apt and see. `sudo apt-get install python-pip` Also, for resizing the image you can use an alternative which is `resizeImage`. This is built in the tkinter package. `img = PhotoImage(file='4 Dry Out Logo.png'); img = resizeImage(img, h, w)` – Jack Oct 12 '21 at 21:38
  • Still an error for pip. Tried the resize but it didn't change anything. The program won't run without the "tktext_label.image = photo" line, is that what's causing the weirdness? – Lady Oct 12 '21 at 21:50
  • 1
    What are you using the variable `photo` for? And where have you defined it? – Jack Oct 12 '21 at 22:03
  • Jack: FYI according to [this comment](https://stackoverflow.com/questions/69547178/how-to-add-image-to-2nd-window-of-tkinter-gui-without-using-pil?noredirect=1#comment122928005_69547178) by (tkinter guru) @BryanOakley, the `PhotoImage` class can do some limited resizing. – martineau Oct 12 '21 at 22:12
  • Yes that is in a way similar to `resizeImage` – Jack Oct 12 '21 at 22:20
  • That means nothing to me — never heard of `resizeImage`. – martineau Oct 12 '21 at 22:22