如何使用Tkinter从输入框获取值 [英] How to get values from entry box with tkinter

查看:218
本文介绍了如何使用Tkinter从输入框获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从打开的输入框中获取值,并使它们成为crop1中的值.我是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

这篇关于如何使用Tkinter从输入框获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆