python - TKinter - 无法更新文本小部件
问题描述
我正在尝试更新一个Text
小部件,但无论我尝试什么都没有更新,也没有错误
def update():# a button calls this
textBox.delete(1.0, tk.END)
textBox.insert(tk.END,"test")
textBox = tk.Text(frame1,height=2,width=10)
textBox.config(state='disabled') #disable editing
textBox.grid(row=0,column=1,pady=2)
解决方案
使用@JacksonPro 的建议
import tkinter as tk
def update():# a button calls this
textBox.config(state="normal") # Make the state normal
textBox.delete("0.0", "end")
textBox.insert("end", "test")
textBox.config(state="disabled") # Make the state disabled again
root = tk.Tk()
textBox = tk.Text(root, height=2, width=10)
textBox.config(state="disabled") #disable editing
textBox.grid(row=0, column=1, pady=2)
button = tk.Button(root, text="Click me", command=update)
button.grid(row=1, column=1)
root.mainloop()
推荐阅读
- unity3d - 纹理呈现粉红色,但仅在远处或某个角度显示时
- ios - 如何仅访问 SwiftUI 中数据的第一个元素?
- mysql - 字符集 utf8mb4 collate utf8mb4_general_ci 不接受带重音的单词
- python - 使用函数和条件将一个数据框列填充到另一个数据框列
- python - How to show column data that contains keyword in a dataframe
- flutter - Instagram 中的视频如何开始自动播放并计算其观看次数?如何在 Flutter 中实现这个功能?
- reactjs - 反应 onClick 只影响这个迭代
- python - Python - 从复杂的 HTML 中自动抓取表格
- r - R:根据条件填充矩阵
- pytorch - Onnx 到 mlmodel 的转换无法生成 .mlmodel 文件