添加到标签时,x像素后的新行(tkinter) [英] 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:
这是我用来写标签的代码示例:
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")
我想知道是否有某种方法可以在经过一定数量的像素(标签宽度)后将其强制到新行,而不必遍历并在所有位置添加"\ n"(因为最后一行是1约有150种可能性).
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).
推荐答案
标签小部件为此提供了一个完美的选择:wraplengt
.
Label widget has a perfect option for that: wraplengt
.
label = Label(parent, text="This is a really long text; " * 5, wraplengt=200)
确定何时将标签的文本换成多行.这以屏幕单位给出.默认值为0(不环绕).
Determines when a label’s text should be wrapped into multiple lines. This is given in screen units. Default is 0 (no wrapping).
这篇关于添加到标签时,x像素后的新行(tkinter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!