Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New line after x pixels when adding to a label (tkinter)

Currently when I write to a label, it will write only one line, and this sometimes disappears off the edge of the label, like so:

Label issues

Here is an example of the code I am using to write to the label:

def updateLabel(self, event):
    global string
    global labelContents
    global windowCommand
    global currentEnvironment
    if event != "no input":
        windowCommand = self.getEntry(event)
        labelDisplay = "> " + windowCommand
        labelContents += labelDisplay
        labelContents += "\n"
        self.checkLabel()
        string.set(labelContents)
        self.textEntry.delete(0, END)
        self.master.after(0, play)
    else:
        self.checkLabel()
        string.set(labelContents)

labelContents += "You have died. Game over." + "\n"
labelContents += "You scored {0}.".format(score) + "\n"
app.updateLabel("no input")

I would like to know if there was any way to force it to a new line after a certain amount of pixels (the label width) without having to go through and add "\n" everywhere (as that last line is 1 of ~150 possibilities).

like image 718
Jon Avatar asked Sep 06 '25 03:09

Jon


1 Answers

Label widget has a perfect option for that: wraplength.

label = Label(parent, text="This is a really long text; " * 5, wraplength=200)

From the Label's documentation on effbot.org:

Determines when a label’s text should be wrapped into multiple lines.
This is given in screen units. Default is 0 (no wrapping).

like image 65
A. Rodas Avatar answered Sep 07 '25 22:09

A. Rodas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!