如何使用tkinter从输入框中获取值

时间:2012-11-11 00:26:26

标签: python python-2.7 tkinter python-imaging-library

我想从打开的输入框中获取值,并将它们变为crop1中的值。我是tkinter和编程的新手,所以我不确定如何将值放入函数中。很抱歉在短时间内有两个问题,这是我今天的最后一个问题。

    from Tkinter import *
from PIL import Image, ImageTk, ImageFilter, ImageEnhance

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        master.wm_title("Image examples")
        self.pack()
        self.createWidgets()
        master.bind('s', self.sharpen)
        master.bind('b', self.brighten)
        master.bind('d', self.darken)
        master.bind('B', self.blur)
        master.bind('r', self.rotate)
        master.bind('I', self.brighten)
        master.bind('c', self.cool)
        master.bind('w', self.warm)
        master.bind('<BackSpace>', self.undo)

def createWidgets(self):
    self.img = Image.open("lineage.jpg")
    self.photo1 = ImageTk.PhotoImage(self.img.convert("RGB"))
    self.label1 = Label(self, image=self.photo1)
    self.label1.grid(row=0, column=0, padx=5, pady=5, rowspan=10)

    self.photo2 = ImageTk.PhotoImage(self.img.convert("RGB"))
    self.label2 = Label(self, image=self.photo2)
    self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)

    button1 = Button(self, text="Brighten", command=self.brighten)
    button1.grid(row=0, column= 2)
    button2 = Button(self, text="Darken", command=self.darken)
    button2.grid(row=1, column= 2, sticky=N)
    button3 = Button(self, text="Warm", command=self.warm)
    button3.grid(row=2, column= 2, sticky = N)
    button4 = Button(self, text="Cool", command=self.cool)
    button4.grid(row=3, column= 2, sticky = N)
    button5 = Button(self, text="Sharpen", command=self.sharpen)
    button5.grid(row=4, column= 2, sticky = N)
    button6 = Button(self, text="Blur", command=self.blur)
    button6.grid(row=5, column= 2, sticky = N)
    button7 = Button(self, text="Rotate", command=self.rotate)
    button7.grid(row=6, column= 2, sticky = N)
    button8 = Button(self, text="Crop", command=self.crop)
    button8.grid(row=7, column= 2, sticky = N)
    button9 = Button(self, text="W/B Levels", command=self.brighten)
    button9.grid(row=8, column= 2, sticky = N)
    button10 = Button(self, text="Undo", command=self.undo)
    button10.grid(row=9, column= 2, sticky = N)



def brighten(self, event = None):
    img2 = self.img.point(lambda p: p * 1.9)
    self.photo2 = ImageTk.PhotoImage(img2)
    self.label2 = Label(self, image=self.photo2)
    self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)

def darken(self, event = None):
    img2 = self.img.point(lambda p: p * 0.5)
    self.photo2 = ImageTk.PhotoImage(img2)
    self.label2 = Label(self, image=self.photo2)
    self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)

def warm(self, event = None):
    img = Image.open("lineage.jpg").convert("RGB")
    (w, h) = (img.size[0], img.size[1])
    img2 = img.copy()
    pixels = img2.load()
    for x in range(w):
        for y in range(h):
            (r, g, b) = pixels[x,y]
            r = int(r*1.3)
            pixels[x,y] = (r, g, b)
    self.photo2 = ImageTk.PhotoImage(img2)
    self.label2 = Label(self, image=self.photo2)
    self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)

def cool(self, event = None):
    img = Image.open("lineage.jpg").convert("RGB")
    (w, h) = (img.size[0], img.size[1])
    img2 = img.copy()
    pixels = img2.load()
    for x in range(w):
        for y in range(h):
            (r, g, b) = pixels[x,y]
            r = int(r/1.3)
            pixels[x,y] = (r, g, b)
    self.photo2 = ImageTk.PhotoImage(img2)
    self.label2 = Label(self, image=self.photo2)
    self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)

def sharpen(self, event = None):
    img2 = self.img.filter(ImageFilter.SHARPEN)
    self.photo2 = ImageTk.PhotoImage(img2)
    self.label2 = Label(self, image=self.photo2)
    self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)

def blur(self, event = None):
    img2 = self.img.filter(ImageFilter.BLUR)
    self.photo2 = ImageTk.PhotoImage(img2)
    self.label2 = Label(self, image=self.photo2)
    self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)

def rotate(self, event = None):
    img2 = self.img.rotate(270)
    self.photo2 = ImageTk.PhotoImage(img2)
    self.label2 = Label(self, image=self.photo2)
    self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)

def undo(self, event = None):
    img2 = self.img
    self.photo2 = ImageTk.PhotoImage(img2)
    self.label2 = Label(self, image=self.photo2)
    self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)



def crop(self):
    self.root = Tk()
    self.root.wm_title("Insert Coords")

    self.x0 = Entry(self.root)
    self.x0.grid()

    self.x1 = Entry(self.root)
    self.x1.grid()

    self.y0 = Entry(self.root)
    self.y0.grid()

    self.y1 = Entry(self.root)
    self.y1.grid()

    Button(self.root, text="Crop", command=self.close_crop).grid()

def close_crop(self):        
    self.crop1(self.x0.get(), self.x1.get(), self.y0.get(), self.y1.get())
    self.root.destroy()                

def crop1(self, x0, x1, y0, y1):
    print x0, x1, y0, y1
    img = Image.open("lineage.jpg").convert("RGB")
    (w, h) = (img.size[0], img.size[1])
    img2 = self.img
    img2 = img2.crop((self.x0,self.x1,self.y0,self.y1))
    self.photo2 = ImageTk.PhotoImage(img2)
    self.label2 = Label(self, image=self.photo2)
    self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)

1 个答案:

答案 0 :(得分:2)

可能有更好的方法,但这很有效。添加一个按钮,当按下该按钮时,从文本条目中获取输入并将它们传递给裁剪功能。

def crop(self):
    self.root = Tk()
    self.root.wm_title("Insert Coords")

    self.x0 = Entry(self.root)
    self.x0.grid()

    self.x1 = Entry(self.root)
    self.x1.grid()

    self.y0 = Entry(self.root)
    self.y0.grid()

    self.y1 = Entry(self.root)
    self.y1.grid()

    Button(self.root, text="Crop", command=self.close_crop).grid()

def close_crop(self):        
    self.crop1(self.x0.get(), self.x1.get(), self.y0.get(), self.y1.get())
    self.root.destroy()                

def crop1(self, x0, x1, y0, y1):
    print x0, x1, y0, y1