python-3.x - 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)
解决方案
当文本小部件被禁用时,您不能在其中输入文本,即使使用.insert()
or 也不行.delete()
。要修改文本,您需要将状态更改为正常,插入文本,然后将状态更改回禁用:
outputwindow.config(state=NORMAL)
outputwindow.insert(END, input)
outputwindow.config(state=DISABLED)
推荐阅读
- docker - Docker conatiner 中的 Ansible 无法通过 ssh 连接到 Hetzner 主机
- python - 根据键对字典进行排序
- angular - 如何使用角度的传单地图将角 45 的弧放置在半径为 3 公里的圆内
- android - Xamarin Forms:如何从另一个应用程序打开一个应用程序?
- azure - New-AzVM 错误 - 属性“securityProfile.encryptionAtHost”无效
- html - Bootstrap 4,在滑块中需要四个项目
- python - SQLalchemy 在捕获异常时回滚?
- node.js - Docker 中的 Nodejs + PhantomJS:错误:spawn - ENOENT
- mongodb - 在 MongoDB 的 Go 客户端中,如何注册 Hook Decoder 来处理接口的解组 BSON?
- colors - 如何检查颜色是否为黑色