Use textvariable for Text box without Entry in TKinter

Viewed 399

I'm writing a GUI program where I want to create text to speech. There I am facing some problem. I am calling textvariable without using Entry Wedge. That's not working. Do you have any solution or alternative? & have you any suggestion to inert text box in GUI or multiline Entry?

Thanks in advance

from tkinter import *
from gtts import gTTS
from playsound import playsound
from tkinter import scrolledtext

root = Tk()
root.geometry("450x400")
root.resizable(0, 0)
root.configure(bg='ghost white')
root.title("TEXT TO SPEECH")

L1 = Label(root, text="TEXT_TO_SPEECH", font="arial 15 bold",
           fg="#20bebe", bg='white smoke').pack()

L2 = Label(root, text="Enter Text", font='arial 10 bold',
           bg='white smoke').place(x=20, y=60)


Msg = StringVar()


textareaframe = Frame(root)
textareaframe.pack()
textareaframe.place(x=20, y=80)

TextArea = Text(textareaframe, textvariable=Msg)
ScrollBar = Scrollbar(textareaframe)
ScrollBar.config(command=TextArea.yview)
TextArea.config(yscrollcommand=ScrollBar.set, width=40, height=10)
ScrollBar.pack(side=RIGHT, fill=Y)
TextArea.pack()

def Convert():
    Message = TextArea.get()
    speech = gTTS(text=Message)
    speech.save('T2S.mp3')
    playsound('T2S.mp3')


def Play():
    pass


def Exit():
    root.destroy()


def Reset():
    Msg.set("")


buttonframe = Frame(root)
buttonframe.pack(padx=5, pady=5)
buttonframe.place(y=280)

Button(buttonframe, text="Convert", font='arial 14 bold',
       command=Convert).pack(side=LEFT, padx=5)

Button(buttonframe, text="PLAY", font='arial 14 bold',
       command=Play).pack(side=LEFT, padx=5)

Button(buttonframe, text='EXIT', font='arial 14 bold',
       command=Exit, bg="#20bebe").pack(side=LEFT, padx=5)

Button(buttonframe, text='RESET', font='arial 14 bold',
       command=Reset).pack(side=LEFT, padx=5)


L_end = Label(text="SAAD QURESHI", font='arial 12 bold', fg="#20bebe",
              bg='white smoke', width='20').pack(side='bottom')

root.mainloop()

output Error

_tkinter.TclError: unknown option "-textvariable"

If I use Entry then its work, but I want to use a multiple line text box.

entry_field = Entry(root, textvariable=Msg, width='50')
entry_field.pack()
entry_field.place(x=20, y=80)   
1 Answers
TextArea = Text(textareaframe)#Remove the textvariable keyword argument
TextArea.pack()

To get the contents of the Text box you can do something like :

contents = TextArea.get('1.0','end')

TextArea.get('1.0','end') returns all the contents of the text box from the 0 index to the last index

Related