繁体   English   中英

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

[英]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()

output 错误

_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')返回文本框从0索引到最后一个索引的所有内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM