Last active
February 25, 2024 01:33
-
-
Save moshekaplan/c425f861de7bbf28ef06 to your computer and use it in GitHub Desktop.
A Logging Handler that allows logging to a Tkinter Text Widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Built-in modules | |
import logging | |
import Tkinter | |
import threading | |
class TextHandler(logging.Handler): | |
"""This class allows you to log to a Tkinter Text or ScrolledText widget""" | |
def __init__(self, text): | |
# run the regular Handler __init__ | |
logging.Handler.__init__(self) | |
# Store a reference to the Text it will log to | |
self.text = text | |
def emit(self, record): | |
msg = self.format(record) | |
def append(): | |
self.text.configure(state='normal') | |
self.text.insert(Tkinter.END, msg + '\n') | |
self.text.configure(state='disabled') | |
# Autoscroll to the bottom | |
self.text.yview(Tkinter.END) | |
# This is necessary because we can't modify the Text from other threads | |
self.text.after(0, append) | |
# Sample usage | |
if __name__ == '__main__': | |
# Create the GUI | |
root = Tkinter.Tk() | |
import ScrolledText | |
st = ScrolledText.ScrolledText(root, state='disabled') | |
st.configure(font='TkFixedFont') | |
st.pack() | |
# Create textLogger | |
text_handler = TextHandler(st) | |
# Add the handler to logger | |
logger = logging.getLogger() | |
logger.addHandler(text_handler) | |
# Log some messages | |
logger.debug('debug message') | |
logger.info('info message') | |
logger.warn('warn message') | |
logger.error('error message') | |
logger.critical('critical message') | |
root.mainloop() |
Author
moshekaplan
commented
Feb 8, 2024
via email
That would require custom styling.
…On Thu, Feb 8, 2024, 4:56 PM lunaeidi ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Are the info/warn/error supposed to show up as different colors by
default? Or do you have to add custom styling for that? Thanks!
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/moshekaplan/c425f861de7bbf28ef06#gistcomment-4884726>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGF2J6YMSIT6Y65YWUK7TTYSVCYFBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVAYTIMRTG42TMNVHORZGSZ3HMVZKMY3SMVQXIZI>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Hello, I'm wondering if you know if this could cause the GUI to freeze? perhaps because of not multithreading? Thanks!
I am not aware of any issues, but I haven't had to use this code in nearly a decade, so it's possible Python 3 has changed something significant that I'm unaware of.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment