2016-12-06 76 views
1

我需要一些帮助,的Tkinter和枕头操作

以下是在Tkinter的显示滑块的代码,

from Tkinter import * 

def show_values(): 
    print (w1.get(), w2.get()) 

master = Tk() 
w1 = Scale(master, from_=0, to=42) 
w1.pack() 
w2 = Scale(master, from_=0, to=200, orient=HORIZONTAL) 
w2.pack() 
Button(master, text='Show', command=show_values).pack() 

mainloop() 

以下是滤波代码

from PIL import ImageFilter 
im2 = im.filter(ImageFilter.MinFilter(3)) 

我想要显示的图像过滤动态,以便当我们滚动滑块和图像得到更新时,传递给MinFilter()的参数应该改变。任何人都可以帮忙吗?

+0

'量表(...,命令= show_values)' – furas

回答

2

Scale具有command=随电流值执行功能从规模

import Tkinter as tk 
from PIL import Image, ImageTk, ImageFilter 


def show_value_1(value): 
    print('v1:', value) 

    # filter image 
    img = image.filter(ImageFilter.MinFilter(int(value))) 

    # create new photo 
    photo = ImageTk.PhotoImage(img) 

    # update image in label 
    l['image'] = photo 

    # PhotoImage has to be assigned to global variable - problem with "garbage collector" 
    l.photo = photo 


def show_value_2(value): 
    print('v2:', value) 


master = tk.Tk() 

image = Image.open("ball-1.png") 
photo = ImageTk.PhotoImage(image) 

l = tk.Label(master, image=photo) 
l.pack() 
l.photo = photo 

w1 = tk.Scale(master, from_=1, to=42, command=show_value_1) 
w1.pack() 

w2 = tk.Scale(master, from_=1, to=200, orient=tk.HORIZONTAL, command=show_value_2) 
w2.pack() 

master.mainloop() 

球1.png

enter image description here

+0

感谢吨。我看到,在l = tk.Label()和l.photo = photo是垃圾引用的额外副本。那是对的吗?我发现http://effbot.org/tkinterbook/photoimage.htm我会在今晚尝试代码。 –

+0

是的,我这样做是因为垃圾收集器的问题,如链接到effbot.org所述。 – furas

+0

非常感谢。代码起作用。 –