PhotoImage没有响应tkinter Scale小部件

时间:2018-09-22 20:22:48

标签: python image tkinter

我有一个Python脚本,允许用户在tkinter画布小部件中打开图像。该程序将打印出新图像应具有的尺寸,但仅显示原始图像。如何以新尺寸刷新画布。

这是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循环和某种处理方法,但是我不确定语法。

编辑 类中的部分代码

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

1 个答案:

答案 0 :(得分:0)

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

此外,除了使用create_image为重新缩放的图像创建新的Canvas图像外,您还应该通过itemconfig更新现有的图像。为此,您需要保存create_image返回的商品ID号。例如,在openit中,

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

然后在创建新的PhotoImage之后,在valuecheck

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

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