[英]Trying to resize image on 2nd window of Tkinter GUI
此代碼將打開一個帶有圖像的主窗口,然后是另一個帶有相同圖像的窗口。 有沒有辦法將圖像調整為更小? (轉到 # !>>> 圖像 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()
錯誤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
來保存對圖像的引用。 保存參考的原因在這個問題的答案中解釋了為什么如果在函數中創建 Tkinter 圖像不顯示?
另外,有沒有辦法將圖像調整為更小?
對該站點的簡單搜索將回答該問題。
我看到 Bryan Oakley 已經發布了你的問題的答案,但我會用我自己的來補充它,這也解決了我在你的代碼中注意到的其他幾個問題(一些與此相關),並展示了如何在不使用 PIL 的情況下調整圖像大小Bryan 在您的相關問題下的評論中提到的subsample()
方法已作為副本關閉。
您可以在 Python 控制台中使用 Python 的內置幫助系統找到有關它的一些文檔, copy()
, zoom()
以及Photoimage
類的其他方法:即
>>> import tkinter
>>> help(tkinter.PhotoImage)
當然,它也在源代碼中。
這是您的問題代碼中的修復代碼:
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
徽標圖像,但這是使用替代圖像在我的系統上運行時單擊按鈕后的情況。
首先,您正在引用未定義為變量的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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.