2

Currently, the text is being printed on a label in one line, but I want it to display on multiple lines instead of one.

def display():
    plain_text="" # this is the text file
    display=Label(top1,text=(plain_text),bg="black", foreground="white")
    display.pack()

So it's currently displayed on one line e.g. somewhereinlamancha.

plain_text is a long string of text and so i can't manually add newlines. It is loaded by the user and the text isn't known before that.

This is in a tkinter GUI.

Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62
katie
  • 21
  • 2
  • 4
    Welcome to stack overflow. Is `Label` part of a library that you imported? Is it Tkinter? You should add that as a tag so the right people will find your question. I am going to add it for you. – tmthydvnprt Mar 13 '16 at 13:55

1 Answers1

0

Newlines

You will need newline characters, \n, in your text variable to have multiple lines on the label.

def display():
    plain_text="this text\nwill be on a\nmultiple lines!"
    display=Label(top1,text=plain_text,bg="black", foreground="white")
    display.pack()
tmthydvnprt
  • 10,398
  • 8
  • 52
  • 72
  • 1
    It would be more appropriate to display a text file in a tkiner `Text` object, since it could be comprised of many long lines of characters, don't you think? – martineau Mar 13 '16 at 14:12
  • the variable plain_text is a long string off text and so i cant /n newline. it is loaded by the user the text isnt known before that. – katie Mar 13 '16 at 14:15
  • @martineau you are probably right. I don't use tkinter just know it's out there and the OP said she was using `Label`. – tmthydvnprt Mar 13 '16 at 14:23
  • 1
    @katie how do you read in the file? You don't need to add newlines necessarily, you just need to know if newlines are in the text string when it gets passed to the `Label`. For example you could be loosing newlines when you accept the string from the user... you should post more of your code on how you get this string. – tmthydvnprt Mar 13 '16 at 14:25
  • katie: The [`textwrap.fill()`](https://docs.python.org/2/library/textwrap.html#textwrap.fill) function could be used to word-wrap the contents of text string so it would fit within a specified max line width (and will insert newlines into the value it returns at the appropriate spots). This would be helpful regardless of whether you're using a `Label` or `Text` widget to display it. with `tkinter`. – martineau Mar 13 '16 at 17:36