Python Logging to Tkinter Text Widget

后端 未结 6 1859
悲哀的现实
悲哀的现实 2020-12-14 16:46

Does any one out there have an example of how to setup logging in Python to a Tkinter Text Widget? I have seen this used in several apps but cannot figure out how to direct

6条回答
  •  忘掉有多难
    2020-12-14 17:38

    You should subclass logging.Handler, e.g.:

    import logging
    from Tkinter import INSERT
    
    class WidgetLogger(logging.Handler):
        def __init__(self, widget):
            logging.Handler.__init__(self)
            self.widget = widget
    
        def emit(self, record):
            # Append message (record) to the widget
            self.widget.insert(INSERT, record + '\n')
    

提交回复
热议问题