usercccc
usercccc

Reputation: 11

How to clear/delete the contents of a Tkinter "ScrolledText" widget?

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~~~

Upvotes: 1

Views: 3564

Answers (1)

scotty3785
scotty3785

Reputation: 7006

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')

Upvotes: 6

Related Questions