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()
Advertisement
Answer
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 .