0

I want to use pyfiglet in tkinter's text widget but it get all jumbled up
what I want is in below image, and this below image is of terminal
enter image description here

But instead of that what I'm getting is
jumbled image

so if Anyone knows how to fix this please let me know
Appreciate your efforts and thanks in advance

1 Answers1

0

As already said in the comment ( by Bryan Oakley ) you need to use a fixed-width font. But be aware that not all in tkinter listed fixed-width fonts behave as such if used as fonts in tkinter. In addition to that on my system the TkFixedFont seems not to have a bold version and doesn't resize. The code below is a bit modified code you find in a link listed at the code begin. After making the figlet text blue and bold I have got what you want:

enter image description here

# http://tcl.tk/man/tcl8.5/TkCmd/text.htm
import tkinter as tk
from   tkinter.font import Font

class Pad(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.buttonFrame = tk.Frame(self)
        self.buttonFrame.pack(side="top", fill="x")

        self.bold_btn  = tk.Button(self.buttonFrame, text="Bold" , command=self.make_bold)
        self.bold_btn.pack(side="left")
        self.blue_btn  = tk.Button(self.buttonFrame, text="Blue" , command=self.make_blue)
        self.blue_btn.pack(side="left")
        self.clear_btn = tk.Button(self.buttonFrame, text="Clear", command=self.allclear)
        self.clear_btn.pack(side="left")

        # Creates a normal and a bold font
        # self.norm_font = Font(font="TkFixedFont", size=14) # no SIZE choice in TkFixedFont? # family="Bitstream Vera Sans Mono" ,weight= , slant= , underline= , overstrike= )
        # self.bold_font = Font(font="TkFixedFont", size=10, weight="bold") # no BOLD version in TkFixedFont? 
        self.norm_font = Font(family="Bitstream Vera Sans Mono", size=14) # font= ,weight= , slant= , underline= , overstrike= )
        self.bold_font = Font(family="Bitstream Vera Sans Mono", size=14, weight="bold") # no BOLD version in TkFixedFont? 

        self.text = tk.Text(self)

        self.text.insert("end", 
        "\n"
        "  Select part of text and then click 'Blue' or/and 'Bold' ...              \n"
        "\n"
        "   ---   ---   ---   ---         ---   ---   ---               ---   ---   \n"
        "  |   | |   | |   |   |         |     |     |   | |\  | |\  | |     |   |  \n"
        "  |-+-  |   | |-+-    |          -+-  |     |-+-| | + | | + | |-+-  |-+-   \n"
        "  |     |   | |  \    |             | |     |   | |  \| |  \| |     |  \   \n"
        "         ---                     ---   ---                     ---           " 
        )
        self.text.focus()
        self.text.pack(fill="both", expand=True)

        # configuring tags called NORM, BLUE, BOLD
        self.text.tag_configure("BOLD", font=self.bold_font)
        self.text.tag_configure("NORM", font=self.norm_font)
        self.text.tag_configure("BLUE", background="blue", foreground="white")
        self.text.tag_add(
            'NORM', # the tag is given the name/id 'NORM' 
            '1.0',  # 1.0 -> first line, first char (lines count from 1, characters from 0)
            'end')  # end of the entire text
        #                                 

    def make_bold(self):
        # tk.TclError exception is raised if not text is selected
        try:
            self.text.tag_remove("NORM", "sel.first", "sel.last")        
            # self.text.tag_remove("BLUE", "sel.first", "sel.last")        
            self.text.tag_add(   "BOLD", "sel.first", "sel.last")        
        except tk.TclError:
            pass

    def make_blue(self):
        # tk.TclError exception is raised if not text is selected
        try:
            # self.text.tag_remove("NORM", "sel.first", "sel.last")        
            # self.text.tag_remove("BOLD", "sel.first", "sel.last")        
            self.text.tag_add(   "BLUE", "sel.first", "sel.last")        
        except tk.TclError:
            pass

    def allclear(self):
        self.text.tag_remove("NORM",  "1.0", 'end')
        self.text.tag_remove("BOLD",  "1.0", 'end')
        self.text.tag_remove("BLUE",  "1.0", 'end')
        self.text.tag_add(   "NORM",  "1.0", "end")


def demo():
    root = tk.Tk()
    root.minsize(width=1056, height=256)
    root.maxsize(width=1056, height=300)
    root.title("PORT SCANNER")
    Pad(root).pack(expand=1, fill="both")
    root.mainloop()


if __name__ == "__main__":
    demo()

P.S. There are questionmarks I have placed in the comments in the above code and there is some info about similar problems I run into with the fixed font issue already myself available on stackoverflow:

How to set a Tkinter widget to a monospaced, platform independent font?

String alignment in Tkinter .

So don't be surprised if the code above doesn't work for you and you have to see which font choice works as expected on your system.

See List available font families in `tkinter` for how to get in tkinter a list of available font families.

Claudio
  • 7,474
  • 3
  • 18
  • 48