简体   繁体   中英

Text in Text Widget (tkinter) doesn't update

I'm currently working with Tkinter, trying to make a text widget that can update its value every second, but I don't know why it doesn't work as expected. I hard-coded the text widget position @@

  • Environment: pipenv, version 2021.11.23. Python 3.9 (32bit)
  • Code:

    self.text = tk.Text(self.root, height=25, width=97)
    self.text.configure(state='disabled')
    self.text.place(x = 10, y = 150)
    
    def update_text(self):
        self.text.delete('1.0', tk.END)
        self.text.insert(tk.END, str(self.i))
        self.i += 1
        self.root.after(1000, self.update_text)
    
    self.update_text()
    self.root.mainloop()

  • Expected output: text widget show a number increasing by 1 every second.
  • What happened instead: nothing appears on the text widget.
  • I tried to insert some text right after calling the configure() function and the number did appear on the screen.

Setting the text widget's state to 'normal' solved the problem.

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