首页 > 解决方案 > tkinter 插入输出文本字段(output.insert())不起作用

问题描述

我有一个 Tkinter GUI,我有一个输入文本字段和输出文本字段和
一个按钮。用户在文本字段中键入任何内容并单击按钮,然后必须将其打印在输出字段中。但是这里输入值没有插入到输出字段中。

from tkinter import *



base = Tk()
base.title('Demo')
base.geometry("400x500")
base.resizable(width=FALSE, height=FALSE)


outputwindow = Text(base, bd=0, bg="white", height="8", width="50", font="Arial",)
# outputwindow.insert(END, "Connecting to your partner..\n")

outputwindow.config(state=DISABLED)

#Bind a scrollbar to the Chat window
scrollbar = Scrollbar(base, command=outputwindow.yview, cursor="heart")
outputwindow['yscrollcommand'] = scrollbar.set


EntryBox = Text(base, bd=0, bg="white",width="29", height="5", font="Arial")


def ClickAction():

    input=EntryBox.get("1.0",END)
    print(input)
    EntryBox.delete('1.0',END)
    outputwindow.insert(END, input)


SendButton = Button(base, font=30, text="Send", width="12", height=5,bd=0, bg="lightgray", command=ClickAction)




#Place all components on the screen
scrollbar.place(x=376,y=6, height=386)
outputwindow.place(x=6,y=6, height=386, width=370)
EntryBox.place(x=128, y=401, height=90, width=265)
SendButton.place(x=6, y=401, height=90)

base.mainloop()

我已经为此目的尝试了其他程序,它工作正常。但是在这里我无法处理,也找不到问题。(我只是在学习 tkinter)

标签: python-3.xtkinter

解决方案


当文本小部件被禁用时,您不能在其中输入文本,即使使用.insert()or 也不行.delete()。要修改文本,您需要将状态更改为正常,插入文本,然后将状态更改回禁用:

outputwindow.config(state=NORMAL)
outputwindow.insert(END, input)
outputwindow.config(state=DISABLED)

推荐阅读