I made a window for Log output with Tkinter

Real-time print debugging is difficult when creating a GUI on Windows with Tkinter I thought it would be more convenient to launch Log Window in addition to the main window, so I created it. Since it is a sub-window to the last, launch the Window at Toplevel. (I don't really understand how to use Tkinter's Toplevel windows) It's not interesting with just Log Window, so I put a check box that can be displayed / hidden depending on the Log type. LogWindow.png

Addendum: Added view ('end') because it was output but was not scrolled to follow the output. Added write (str) and flush () to support python standard output

You can use it as sys.stdout = Logwindow.

IOLogWindow.py


import sys
import serial
import binascii
import time
import tkinter as tk
from tkinter import scrolledtext

TEXT_COLORS = {
    'MESSAGE' : 'black',
    'INPUT' : 'blue',
    'OUTPUT' : 'green',
    'ERROR' : 'red',
    'DEBUG' : 'yellow'    
    }

# Initial value = flag=True or False
class SimpleCheck(tk.Checkbutton):
    def __init__(self, parent, *args, **kw):
        self.flag = kw.pop('flag')
        self.var =  tk.BooleanVar()
        if self.flag:
            self.var.set(True)
        self.txt = kw["text"]
        tk.Checkbutton.__init__(self, parent, *args, **kw, variable=self.var)

    def get(self):
        return self.var.get()

class IOLogFrame(tk.Frame):
    def __init__(self, master):    
        tk.Frame.__init__(self, master)
        master.title("Log Window")

        #view/hide choice
        select_frame = tk.LabelFrame(master, text= "Log text disable",relief = 'groove')

        self.ckboxs = []
        for key in TEXT_COLORS:
            cb = SimpleCheck(select_frame, text=key, command=self.callback, flag=False)
            self.ckboxs.append(cb)
            cb.pack(side='left')
        select_frame.pack(side = 'top', fill = 'x')
        
        self.txt = scrolledtext.ScrolledText(master)
        self.txt.pack(fill=tk.BOTH, expand=1)
        for key in TEXT_COLORS:
            self.txt.tag_config(key, foreground=TEXT_COLORS[key])

    def callback(self):
        count = 0
        for key in TEXT_COLORS:
            if(self.ckboxs[count].get()):
                self.hide(key)
            else:
                self.view(key)
            count += 1

    def print(self, str, state='MESSAGE'):
        self.txt.insert(tk.END, str+'\n', state)
        self.txt.see(tk.END)

    def hide(self, tag):
        self.txt.tag_config(tag, elide=True)

    def view(self, tag):
        self.txt.tag_config(tag, elide=False)

    def write(self, str, state='MESSAGE'):
        self.txt.insert(tk.END, str+'\n', state)
        self.txt.see(tk.END)

    def flush(self):
        pass

#sample main window
class IOLogWindow(tk.Toplevel):
    def __init__(self, master):
        master.title("Main WIndow")
        tk.Toplevel.__init__(self, master)
        io = IOLogFrame(self)
        io.print("Message")
        io.print("--ERROR--", 'ERROR')
        io.print("--INPUT--", 'INPUT')
        io.print("--OUTPUT--", 'OUTPUT')
        io.print("--DEBUG--", 'DEBUG')    
        
if __name__ == '__main__':
    win = tk.Tk()
    io=IOLogWindow(win)
    win.mainloop()
    


Recommended Posts

I made a window for Log output with Tkinter
[Python] I made a Youtube Downloader with Tkinter.
I made a fortune with Python.
I made a daemon with Python
I made a resource monitor for Raspberry Pi with a spreadsheet
I made a puzzle game (like) with Tkinter in Python
I made a dash docset for Holoviews
I made a character counter with Python
I made a lot of files for RDP connection with Python
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
I made a roguelike game with Python
I made a program that automatically calculates the zodiac with tkinter
I made a simple blackjack with Python
I made a configuration file with Python
I made a library for actuarial science
I made a WEB application with Django
I made a neuron simulator with Python
I made a python dictionary file for Neocomplete
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a spare2 cheaper algorithm for uWSGI
I made a useful tool for Digital Ocean
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
Procedure for creating a LineBot made with Python
I made a simple Bitcoin wallet with pycoin
I made a downloader for word distributed expression
I made a LINE Bot with Serverless Framework!
I made a random number graph with Numpy
I made a peeping prevention product for telework.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a QR code image with CuteR
〇✕ I made a game
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
[AWS] I made a reminder BOT with LINE WORKS
I made a household account book bot with LINE Bot
I made a user management tool for Let's Chat
I made a Christmas tree lighting game with Python
I made a vim learning game "PacVim" with Go
I made a cleaning tool for Google Container Registry
I made a net news notification app with Python
I made a VM that runs OpenCV for Python
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
[Python] I made a classifier for irises [Machine learning]
I tried a simple RPA for login with selenium
I made a falling block game with Sense HAT
I made blackjack with python!
Output large log with discord.py
I made a python text
Made a command for FizzBuzz
I made a discord bot
I measured BMI with tkinter
I made COVID19_simulator with JupyterLab
I made Word2Vec with Pytorch
I made blackjack with Python.
I made wordcloud with Python.