简体   繁体   中英

Update a Tkinter Text widget real time?

from Tkinter import *

class Output:
    def __init__(self,master):
    self.u=Text(master,width=40)
    self.u.grid(row=0,column=0)
    self.v=Button(master,text="Add text",command="Write")
    self.v.grid(row=1,column=0)

def Write(self):
    self.u.insert(1.0,"Meh")

root=Tk()
output=Output(root)
root.mainloop()

How do I make the button work in real time? If possible, I'd like to have an explanation why this won't work.

I don't understand what you mean by "work in real time", but I definitely see a bug in your code. The command option takes a reference to a command but you're giving it a string. Change the button definition to this:

self.v = Button(master, text="Add text", command=Write)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM