I want to create a log window where I show records of error or success messages.
Please tell me any solution to insert text in text_box from any widget in current application.
Is any solution like we use textvariable
Purpose: Suppose we have two tabs, in one we are downloading something and some error comes in it, then this error is inserted in the text box of the other tab.
I am updating my question to get better answer.
File Structure:
downloader.py
import tkinter as tk
class DownloadWindow(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
################# self configure ##########################
self.columnconfigure(0, weight=1)
############### entry ################################
self.entry = tk.Entry(self)
self.entry.grid(row=0, column=0, sticky='nsew')
self.entry.insert(0, 'I am error send me to Logs...')
################ Button #################################
self.button = tk.Button(self, text='Send to Log')
self.button.grid(row=1, column=0, sticky='nsew')
insert.py
"""
Help me to write a function in this module for button in downloader
"""
logs.py
import tkinter as tk
class LogWindow(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
################# self configure ##########################
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.text_box = tk.Text(self, wrap=None, bg='black', font='roboto 14 bold')
self.text_box.grid(row=0, column=0, sticky='nsew')
self.scroller = tk.Scrollbar(self, command=self.text_box.yview, orient='vertical')
self.text_box.configure(yscrollcommand=self.scroller.set)
self.scroller.grid(row=0, column=1, sticky='nse')
self.text_box.tag_configure('welcome', foreground='white', background='black')
self.text_box.tag_configure('error', foreground='red')
self.text_box.tag_configure('success', foreground='green')
self.text_box.insert('end', '>>> Welcome to Log Window', 'welcome')
self.text_box.insert('end', '\n>>> Completed', 'success')
self.text_box.insert('end', '\n>>> Something Wrong in Data Download', 'error')
luncher.py
import tkinter as tk
from tkinter import ttk
from Test.logs import LogWindow
from Test.downloader import DownloadWindow
root = tk.Tk()
base_tab = ttk.Notebook(root)
base_tab.grid(row=0, column=0, sticky='nsew')
base_tab.add(DownloadWindow(base_tab), text='Download')
base_tab.add(LogWindow(base_tab), text='Logs')
root.mainloop()
Screenshot:
Final Update and Goal Achived
Added In downloader.py
self.button.config(command=self.create_log)
def create_log(self):
write_log(self.entry.get())
Added In insert.py
def write_log(text):
with open('logs_data.txt', 'a') as log:
log.write(f'\n{text}')
Added In logs.py
self.show_log()
def show_log(self):
global file_name, cached_stamp
stamp = os.path.getmtime(file_name)
if stamp != cached_stamp:
with open(file_name, 'r') as log:
data = log.readlines()
self.text_box.insert('end', f'\n{data[-1]}', 'success')
cached_stamp = stamp
self.after(1000, self.show_log)
Conclusion
I am appending each log in .txt file and reading them in every sec. from LogWindow and detecting change. If change occur then inserting in Text widget.



self.text_box.insert()— the same way you do it in theWindows.__init__()method,Text-like widget that supports atextvariable=option. However it's (still) unclear how having one fits into what you want in theinsertmodule. None of your code contains any clues as to the interface you desire it to have (other than maybe it contains a function of some sort). You seem to have an idea, but we can't read your mind…insertmethod on the text widget.insertis a function. This is literally the only way to insert text into a widget.