首页 > 解决方案 > 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)

标签: pythontkinter

解决方案


使用@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()

推荐阅读