1

Hi I am new to the tkinter module, I am trying to get the text entered into the textbox, so that the editimage() will write that text on the specified image.

Here is the Code:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw, ImageFont
import tkinter
from tkinter import filedialog as fd
from tkinter import messagebox as mb


def callback():
    name = fd.askopenfilename()
    global filename
    global input_text
    filename = name.replace('/', '\\')
    if filename:
        Window.geometry('500x250')
        tkinter.Label(Window,
                      text='File has been selected, now click on edit image'
                      ).pack(fill=tkinter.X, padx=5, pady=5)
        tkinter.Label(Window, text='Enter text').pack(fill=tkinter.X,
                padx=5, pady=5)

        # I am unable to get the text using this. Need to pass it to the editimage() to write this text on an image.

        inputtxt = tkinter.Text(Window, height=3, width=25)
        inputtxt.pack()
        input_text = inputtxt.get('1.0', 'end-1c')

        tkinter.Button(Window, text='Edit Image',
                       command=editimage).pack(fill=None, padx=15,
                pady=15)


def editimage():
    try:
        image = Image.open(filename)

        # initialise the drawing context with
        # the image object as background

        (width, height) = image.size
        draw = ImageDraw.Draw(image)
        color = 'rgb(255, 255, 255)'
        xy = [0, height - 50, 942, 586]
        draw.rectangle(xy, fill='white', outline=None)

        # create font object with the font file and specify
        # desired size

        font = ImageFont.truetype('C:\\Windows\\Fonts\\Times.ttf',
                                  size=22)

        # starting position of the message

        (x, y) = (width / 2 - width * 0.3, height - 40)
        message = input_text
        colour = 'rgb(0, 0, 0)'  # black color

        # draw the message on the background

        draw.text((x, y), message, fill=colour, font=font)

        # save the edited image

        image.save('Edited.png')
        mb.showinfo('Photo-Editor',
                    'The Photo has been edited succesfully')
    except Exception, e:
        print 'Failed to edit the image'
        print str(e)


Window = tkinter.Tk()
Window.geometry('500x90')
Window.title('Photo-Editor')
errmsg = 'Error!'
tkinter.Label(Window,
              text='Click on Browse for selecting the image file'
              ).pack(fill=tkinter.X, padx=5, pady=5)

tkinter.Button(Window, text='Browse', command=callback).pack(fill=None,
        padx=5, pady=5)
Window.mainloop()

I tried printing it as well, but I am getting an empty string. ANy help will be greatly appreciated.

3
  • You call input_text = inputtxt.get(...) just after inputtxt = tkinter.Text(...). You need to give some time to the user to enter the data in the text box. You should move the input_text = inputtxt.get(...) inside editimage Commented May 4, 2021 at 9:19
  • Hey thanks a lot. I moved it to the editimage(). After doing this it is working fine. Thanks a ton for the help! Commented May 4, 2021 at 9:59
  • I have another question. How can we reset the window again? What I mean is what if I want to select another image and edit that. When I do that now, I am seeing another textbox getting added to the tkinter window. Typing it in the second one will make sure it works, but this is a problem if I have to do this for multiple images. I understand that this is because of the condition I have in place to show the textbox is by clicking on browse and selecting an image. Is there any way I can make sure only one textbox is shown always? Commented Jun 26, 2021 at 8:14

1 Answer 1

1

This shouldn't be too hard actually, and your code looks fine to me, I don't understand why it isn't working. Refer to the following code:

from tkinter import *

def command():
  input1 = textarea.get("1.0", "end-1c")
  print(input1)

root = Tk()

textarea = Text(root)
textarea.pack()

btn = Button(root, text="Click here", command=command)
btn.pack()

root.mainloop()

That code works for me. Hopefully for you too. Let me know if not. Thanks, and happy coding!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.