试图在Tkinter GUI的第二个窗口上调整图像大小

2024-10-05 10:50:18 发布

您现在位置:Python中文网/ 问答频道 /正文

此代码将打开一个带有图像的主窗口,然后打开另一个带有相同图像的窗口。有没有办法将图像的大小调整为更小? (转到图像2(第二个窗口))

代码如下:

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()

Tags: 图像imageselfmastertitlebluerootout
3条回答

我看到Bryan Oakley已经发布了一个关于你问题的答案,但我将用我自己的补充,它还修复了我在你的代码中注意到的几个其他问题(一些与this有关),并展示了如何使用Bryan在comment中提到的subsample()方法调整图像大小而不使用PIL在你的相关问题下,该问题已作为副本关闭

通过使用Python控制台中的Python内置帮助系统,您可以找到一些关于它的文档,copy()zoom(),以及Photoimage类的其他方法:即

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

当然,它也在source code

以下是问题代码中的代码以及修复程序:

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()

我没有您的4 Dry Out Logo.png徽标图像,但以下是在我的系统上使用替代图像运行时单击按钮后的情况

screenshot

错误NameError: name 'photo' is not defined来自此行:

tktext_label.image = photo

正如错误所说,您从未定义过photo。我猜您只是从某个地方复制了这段代码,而不了解代码在做什么。在本例中,您复制的代码试图保存对先前创建的图像的引用。您重命名了图像或更改了此语句中的名称,从而导致了错误。它与创建第二个窗口无关

代码应该类似于以下内容,不过我添加了一些注释以显示需要使用相同名称的三个位置:

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

您的问题是,您在最后一行中使用了photo,而您本应使用img

代码的要点是,在创建图像后,它通过将图像指定给tktext_label.image来保存对图像的引用。保存引用的原因在问题Why does Tkinter image not show up if created in a function?this answer中解释

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

A simple search of this site将回答这个问题

首先,您引用的是未定义为变量的photo(除非它位于另一个文件中,并且您尚未导入它)。这就是错误的来源

要调整图像大小,您需要PIL包-pip install pillow

在此之后,您可以导入它并以以下方式使用它:

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)

相关问题 更多 >

    热门问题