TKinter - 无法更新文本小部件

时间:2021-03-13 11:49:59

标签: 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)

1 个答案:

答案 0 :(得分:1)

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