1

I am trying to print table via PrettyTable library in python in my ScrolledText tkinter widget. here is the function snippet:

global Scrolledtext1
Scrolledtext1 = ScrolledText(top, state="disable")

def display_output(self,output_message):
        myTable = PrettyTable()
        
        for list_item in output_message:
            myTable.add_row(list_item)
            
        print(myTable)
        
        Scrolledtext1.config(state="normal")        # changing text box's state so that we can edit it
        Scrolledtext1.delete("1.0", "end")          # deletes previous contents
        Scrolledtext1.insert(1.0,myTable)           # showing output message
        Scrolledtext1.config(state="disable")       # disabling text box's state so that user cannot change it's content

I am inserting myTable in second last line in above code (in insert method). this is printing table correctly in console via print statement but it is not printing correctly in ScrolledText . This is screenshot of this table which is inserted in ScrolledText in tkinter:

GUI output image

enter image description here

console output image

enter image description here

Please suggest me something so that i can print table correctly in ScrolledText.

James Z
  • 12,209
  • 10
  • 24
  • 44
Aayush Shah
  • 584
  • 1
  • 8
  • 17
  • 1
    You need to use a fixed width font. – Bryan Oakley Mar 29 '21 at 15:44
  • 1
    Pick one of [these](https://en.wikipedia.org/wiki/List_of_monospaced_typefaces) fonts. They are monospaced like in your console. Make sure that font is available on your computer. You can use the `font` parameter in your widget initialisation `ScrolledText(top, state="disable")` like this: `ScrolledText(top, state="disable", font=(, ))` – TheLizzard Mar 29 '21 at 15:51
  • @BryanOakley How to use fixed width font? Could you please elaborate with an example? – Aayush Shah Mar 30 '21 at 03:18

0 Answers0