how to direct python logger to Tkinker's Listbox?
logging, python, tkinter
Solution
in this case it's probably best to implement your own logging.Handler:
from logging import Handler, getLogger
class ListboxHandler(Handler):
def __init__(self, box):
self._box = box
Handler.__init__(self)
def emit(self, record):
r = self.format(record)
self._box.insert(0, r)
# quick test:
target = [] # supports insert like Listbox :)
rootLogger = getLogger()
# add handler to the root logger here
# should be done in the config...
rootLogger.addHandler(ListboxHandler(target))
rootLogger.warn('test')
print(target)
this way you have full control over formatting, log levels etc. from your config.
Problem
I have a simple app with a class representing a data structure and a class for GUI. I use a logger inside the first class: ``` class A(object): def __init__(self): self.logger = logging.getLogger(self.__class__.__name__) self.logger.info('creating new A object') ``` etc. The GUI consists in one Tkinter window with a Listbox. How can I direct the logs to the listbox? I would like to see the messages populate the list instead as they get logged instead of showing up in the console or log file. How can I update the listbox while a method within the class is executed?