在 TKinter 中对没有条目的文本框使用文本变量

Use textvariable for Text box without Entry in TKinter

我正在编写一个 GUI 程序,我想在其中创建文本到语音。在那里我遇到了一些问题。 我在不使用 Entry Wedge 的情况下调用 textvariable。 这不起作用。您有任何解决方案或替代方案吗? & 您对在 GUI 或多行输入中插入文本框有什么建议吗?

提前致谢

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()

输出错误

_tkinter.TclError:未知选项“-textvariable”

如果我使用 Entry 那么它的工作,但我想使用多行文本框。

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

要获取文本框的内容,您可以执行以下操作:

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

TextArea.get('1.0','end')returns文本框从0索引到最后一个索引的所有内容