简体   繁体   中英

How to get values from entry box with tkinter

I want to take the values from the entry box that opens and have them become the values in crop1 . I'm new to tkinter and programming so I'm not sure on how to get the values into the function. Sorry for two questions in a short period of time, this is my last one for today.

    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)

There is probably a nicer way to do it, but this works. Add a button, and when that button is pressed get the input from the text entries and pass them to the crop function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM