0

I'm using python tkinter to make a GUI, and I use a ScrolledText widget to save log (I followed this link: https://beenje.github.io/blog/posts/logging-to-a-tkinter-scrolledtext-widget/).

I want to clear the ScrolledText when hitting a rerun button and log new log to the ScrolledText.

My code:

import tkinter as tk
import tkinter.scrolledtext

class MainPage(tk.Frame)
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        self.button_start = tk.Button(self, text='START', command=self.on_rerun_button)
        self.button_start.grid(row=7, column=0, columnspan=2, padx=5, pady=5, sticky='NSWE')

        self.scrolledtext_log = tk.scrolledtext.ScrolledText(self, state='disabled')
        self.scrolledtext_log.configure(font='TkFixedFont')
        self.scrolledtext_log.grid(row=0, column=2, rowspan=8, columnspan=8, sticky='NSWE')
   
    def on_rerun_button(self):
        self.scrolledtext_log.delete('1.0', tk.END) #but it doesn't work here

        # run something here...

Please help me, thanks a lot~~~

usercccc
  • 1
  • 2
  • how do you call `on_rerun_button`? Maybe this helps: [Why is the command bound to a Button or event executed when declared?](https://stackoverflow.com/questions/5767228/why-is-the-command-bound-to-a-button-or-event-executed-when-declared) – Matiiss Oct 27 '21 at 08:02
  • I just updated my code. I used `self.button_start = tk.Button(self, text='START', command=self.on_rerun_start)` – usercccc Oct 27 '21 at 08:25
  • 1
    You can't delete text from a disabled text box. Simple. – scotty3785 Oct 27 '21 at 08:36
  • [Setting the state to -disable disables inserts and deletes.](https://wiki.tcl-lang.org/page/Read%2Donly+text+widget). – Henry Yik Oct 27 '21 at 08:37

1 Answers1

5

You need to enable the text widget for editing. In your function you can temporarily re-enable the widget and then disable it again.

def on_rerun_button(self):
    self.scrolledtext_log.configure(state='normal')
    self.scrolledtext_log.delete('1.0', tk.END)
    self.scrolledtext_log.configure(state='disabled')
scotty3785
  • 6,763
  • 1
  • 25
  • 35