2016-12-06 101 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

非常感謝。代碼起作用。 –