python - PhotoImage 没有响应 tkinter Scale 小部件

标签 python image tkinter

我有一个 python 脚本,它允许用户在 tkinter Canvas 小部件中打开图像。程序正在打印新图像应有的尺寸,但只显示原始图像。如何使用新尺寸刷新 Canvas 。

这是Python代码:

from Tkinter import * 
import tkFileDialog
from PIL import ImageTk, Image
factor = 1.0
width_org = 500
height_org = 500
mGui = Tk()
mGui.geometry("600x600")
mGui.configure(background = 'yellow')
frame = Frame(mGui, width = 400, height = 400)
frame.pack(anchor = 'nw')
frame.configure(background = 'red')
canvasframe = Canvas(frame, width = 400, height = 400, scrollregion = (-500,-500,500,500))
hbar = Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM, fill = X)
hbar.configure(command= canvasframe.xview)
vbar = Scrollbar(frame,orient = VERTICAL )
vbar.pack(side=RIGHT, fill = Y)
vbar.configure(command= canvasframe.yview)
canvasframe.configure(xscrollcommand= hbar.set, yscrollcommand= vbar.set)
canvasframe.pack( expand = True, fill = BOTH)
pil_img = Image.open("rose-flower-500x500.jpg")
img = ImageTk.PhotoImage(pil_img)
def openit():
    in_path = tkFileDialog.askopenfilename()
    try:
        pil_img = Image.open(in_path)
    except IOError:
        pass
    width_org, height_org = pil_img.size
    try:
        img = ImageTk.PhotoImage(pil_img)
    except IOError:
        pass

    canvasframe.create_image(20,20, image = img)
    canvasframe.img = img
valueList = [25, 50, 100, 150, 200]
def valuecheck(value):
    newval = min(valueList, key=lambda x:abs(x-float(value)))
    scalepic.set(newval)
    factor = newval/100.0
    w = int(width_org*factor)
    h = int(height_org*factor)
    print (w, h)
    pil_img = pil_img.resize((w,h),Image.ANTIALIAS)
    img = ImageTk.PhotoImage(pil_img)
    canvasframe.create_image(20,20, image =img)
openpic = Button(mGui, text = "Open", command = openit).pack()
scalelabel = Label(mGui, text = "Resize Image").pack()
scalepic = Scale(mGui, from_=min(valueList), to=max(valueList), command=valuecheck, orient = HORIZONTAL)
scalepic.set(100)
scalepic.pack() 

mGui.mainloop()

另外,如何打开新图像?我正在考虑 for 循环和某种 dispose 方法,但我不确定语法。

编辑 类中的部分代码

from Tkinter import *
import tkFileDialog
from PIL import ImageTk, Image


factor = 1.0
width_org = 500
height_org = 500
class MainApp(Frame):
    def createControls(self):
        frame = Frame(self, width = 600, height = 500)
        frame.configure(background = 'red')
        frame.pack(anchor = 'nw')
        canvasframe = Canvas(frame, width = 600, height = 500, scrollregion = (-600,-500,600,500))
        canvasframe.configure(xscrollcommand= hbar.set, yscrollcommand= vbar.set)
        canvasframe.pack(expand = True, fill = BOTH)
        hbar = Scrollbar(frame, orient=HORIZONTAL)
        hbar.pack(side=BOTTOM, fill=X)
        hbar.configure(command= canvasframe.xview)
        vbar = Scrollbar(frame, orient=VERTICAL)
        vbar.pack(side=RIGHT, fill = Y)
        vbar.configure(command= canvasframe.yview)


    def __init__(self, parent):
        Frame.__init__(self, parent, width = 800, height = 600, background = 'yellow')
        self.pack()
        self.createControls()


root = Tk()
app = MainApp(parent =root)

app.mainloop()

最佳答案

您忘记在 canvasframe.img 中保存对新 PhotoImage 的引用,因此当 valuecheck 返回时它会被垃圾收集。

此外,您只需通过 itemconfig 更新现有图像,而不是使用 create_image 为重新缩放的图像创建新的 Canvas 图像。为此,您需要保存 create_image 返回的项目 ID 号。例如,在 openit 中,执行

canvasframe.imgid = canvasframe.create_image(20,20, image = img)

然后在 valuecheck 中,创建新的 PhotoImage 后执行

canvasframe.img = img
canvasframe.itemconfig(canvasframe.imgid, image=img)

您不必将 imgimgid 存储为 canvasframe 的属性,但在这种情况下很方便。但如果 Canvas 上有很多需要跟踪的东西,就会变得困惑。但对于较大的 Tkinter 程序,最好将所有内容放入一个或多个类中。它使事情更有条理,更容易访问。

关于python - PhotoImage 没有响应 tkinter Scale 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52460584/

相关文章:

python - 为什么这个除法没有正确执行?

python - Scipy 稀疏矩阵求幂 : a**16 is slower than a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a?

python - 在以下Python脚本中需要帮助

python - 如何让 Matplotlib 图在 Tkinter GUI 中正确滚动和调整大小

python - tkinter - 使用 wm_manage 和 wm_forget 停靠和取消停靠框架

python - 将一个数组作为索引传递给另一个数组?

导入模块的 Python 作用域和命名空间

html - 如何在 react js中将类型数据缓冲区转换为图像

image - 将图像链接发送到 Telegram 而不显示图像 url

python - 如何在 Python 的 Tkinter 中制作一个交互式列表,并包含可以编辑这些列表的按钮?