Sam
Sam

Reputation: 3

Tkinter Label is give me random tabbing of text when using \n

I'm trying to create a label from python's Tkinter that will store information eventually. Right now I'm just using a place holder but it's tabbing in the text when I enter the text down with \n.

What the Label looks like with the current code: Tkinter Label without the text correctly aligned

And here's the code which is used to make the label:

StoryFrame = LabelFrame(root, width = 1000, heigh = 300, bg="DarkGrey", highlightbackground='black', text='Current News in Technology', font="Helvetica 15 bold")

StoryFrame.place(relx=0.03, rely= 0.65)

info = Label(StoryFrame, anchor=NW, text="Dateline: Date and/or time\n\nNews source: Name\n\nHostname: Domain\n\nURL: Address", width=40, height=17)

info.place(relx=0.685, rely= 0.01)

Upvotes: 0

Views: 57

Answers (2)

user14963647
user14963647

Reputation:

from tkinter import *

root = Tk()

StoryFrame = LabelFrame(root, width = 1000, heigh = 300, bg="DarkGrey", highlightbackground='black', text='Current News in Technology', font="Helvetica 15 bold")

StoryFrame.place(relx=0.03, rely= 0.65)

info = Label(StoryFrame, anchor=NW, text="Dateline: Date and/or time\n\nNews source: Name\n\nHostname: Domain\n\nURL: Address", width=40, height=17, justify='left')

info.place(relx=0.685, rely= 0.01)

root.mainloop()

This code gives the output you want. If have added justify='left' to align it to left.

Upvotes: 0

acw1668
acw1668

Reputation: 46687

It is because the default value of justify option is "center". If you want to align the lines at the left, add justify="left" in Label():

info = Label(StoryFrame, anchor=NW,
             text="Dateline: Date and/or time\n\nNews source: Name\n\nHostname: Domain\n\nURL: Address",
             width=40, height=17, justify="left")

Upvotes: 1

Related Questions