Home>

I want to display the selected txt data in a text box and change the characters in the txt data to red ➡ green ➡ red ➡ green every second.

[Problems that are occurring]
No error statement occurs and you do not know what is wrong.
I'm having trouble executing txt.delete and txt.insert in the file_read function.
I would like to know what is wrong and how to execute insert and delete.
Thank you.
Corresponding source code
import tkinter as tk
import tkinter.filedialog
import time
class Application (tkinter.Tk):
    def __init __ (self):
        super () .__ init __ ()
        self.txt = tk.Text (height = 20, width = 50)
        self.txt.pack ()
        self.txt.tag_configure ("r", foreground = "# e41a1c")
        self.txt.tag_configure ("g", foreground = "# 00FF00")
        self.txt.configure (state = "disabled")
        self.read_button = tkinter.Button (self, text ='change', command = self.file_read)
        self.read_button.pack ()
    def file_read (self):
        file_path = tkinter.filedialog.askopenfilename ()
        if len (file_path)! = 0:
            f = open (file_path)
            data = f.read ()
            for j in range (0,4):
                self.txt.delete ("1.0", "end")
                self.txt.insert ("end", data,'r')
                self.txt.update ()
                time.sleep (1)
                self.txt.delete ("1.0", "end")
                self.txt.insert ("end", data,'g')
                self.txt.update ()
            f.close ()
        else: else:
            data =''''
        return data
app = Application ()
app.mainloop ()
What I tried

I wonder if it will not be executed unless self.txt is declared in the file_read function
self.txt = tk.Text (height = 20, width = 50)
self.txt.pack ()
I tried to write in the file_read function, but nothing was displayed in the textbox without any errors.

Supplementary information (FW/tool version, etc.)

python3.7