'Python with matplotlib and tkinter is working but I don’t understand how

I’m writing a program to help me solve small puzzles, like Sudoku. I’m a newbie at Python and am just starting to use matplotlib and tkinter. I’m confused as to how they are working together.

I show a board using matplotlib – that works fine (I’ve taken away most of the detail in the code below). I then want to show a big block of text – along with scroll bars if necessary. I couldn’t work out how to do this in matplotlib so I tried to use tkinter. I experimented with various options and was surprised to find that the text miraculously appeared in my matplotlib window – even though I thought I was sending it to tkinter!

I’m happy with what this is doing for now but I need to understand it better so I can adapt how it operates in line with my needs.

Please can you help me to understand what is happening on the lines marked “[to explain]”?

def display_board():

    fig = plt.figure(figsize=(6, 6))
    ax = fig.add_subplot(111)                   # [to explain]
    plt.xlim(0,6)
    plt.ylim(0,6)
    plt.grid(True)
    plt.text(3.4,2.9, 'hello', fontname='serif', fontsize='small', color='green')
    rect = ax.add_patch(patches.Rectangle(xy=(3,2), width=1, height=1, color=(0.69, 0.34, 0.15, 0.5), fill=True, zorder=2))
    plt.show(block=False)

    #root = tk.Tk()
    txt = 'blah blah blah blah blah blah blah blah blah blah blah blah \n'
    txt = txt+txt+txt+txt+txt+txt+txt+txt+txt+txt+txt+txt+txt+txt+txt+txt+txt
    txt = txt+txt
    TKScrollTXT = tkscrolled.ScrolledText()     # [to explain]
    TKScrollTXT.insert(1.0, txt)                # [to explain]
    TKScrollTXT.pack(side=tk.LEFT)              # [to explain]
    #root.mainloop()

    plt.show(block=True)


if __name__ == '__main__':
    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    import numpy as np
    import tkinter as tk
    import tkinter.scrolledtext as tkscrolled

    display_board()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source