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.
input_text = inputtxt.get(...)just afterinputtxt = tkinter.Text(...). You need to give some time to the user to enter the data in the text box. You should move theinput_text = inputtxt.get(...)insideeditimage