Home > Software design >  Why does tkinter remove spaces in label?
Why does tkinter remove spaces in label?

Time:02-14

I am completely new to python and I added a label

l1 = Label(master, text = """

##     ##       ###        ######     ########    ########    ########     ##     ##    ####    ##   ##    ########  
### ###      ## ##      ##    ##       ##       ##          ##     ##    ###   ###     ##     ###   ##    ##     ## 
#### ####     ##   ##     ##             ##       ##          ##     ##    #### ####     ##     ####  ##    ##     ## 
## ### ##    ##     ##     ######        ##       ######      ########     ## ### ##     ##     ## ## ##    ##     ## 
##     ##    #########          ##       ##       ##          ##   ##      ##     ##     ##     ##  ####    ##     ## 
##     ##    ##     ##    ##    ##       ##       ##          ##    ##     ##     ##     ##     ##   ###    ##     ## 
##     ##    ##     ##     ######        ##       ########    ##     ##    ##     ##    ####    ##    ##    ########  
""")

# grid method to arrange labels in respective
# rows and columns as specified
l1.grid(row = 0, column = 0, pady = 2)

But the output I get is quite different than printing MASTERMIND, output: Unwanted output Why is this happening? I tried Text widget and with correct height and width it worked but I wanted to ask why doesn't it work with Label widget

CodePudding user response:

You need to use monospaced font, like 'Courier', and set justify='left' in the Label widget:

l1 = Label(master, text = """

##     ##       ###        ######     ########    ########    ########     ##     ##    ####    ##    ##    ########
###   ###      ## ##      ##    ##       ##       ##          ##     ##    ###   ###     ##     ###   ##    ##     ##
#### ####     ##   ##     ##             ##       ##          ##     ##    #### ####     ##     ####  ##    ##     ##
## ### ##    ##     ##     ######        ##       ######      ########     ## ### ##     ##     ## ## ##    ##     ##
##     ##    #########          ##       ##       ##          ##   ##      ##     ##     ##     ##  ####    ##     ##
##     ##    ##     ##    ##    ##       ##       ##          ##    ##     ##     ##     ##     ##   ###    ##     ##
##     ##    ##     ##     ######        ##       ########    ##     ##    ##     ##    ####    ##    ##    ########
""", font='Courier 8', justify='left')

Result:

enter image description here

  • Related