如何在使用可变大小的标签中分隔文本

时间:2019-03-03 22:15:32

标签: tkinter tkinter-canvas

我的程序需要用户输入,输入可能会很长,程序的一部分也显示该文本,到目前为止,我有管理员来获取要显示的文本,但是,文本执行的是固定大小屏幕

我怎样才能使我的程序分隔文本,使其只能走那么远,然后消息在下一行继续 image shows part of the intended message

    cursor.execute(findPlayerTeamname, [(playerTeamname)])
    temp = cursor.fetchall()
    temp = temp[0][0]

    message = StringVar()
    message.set(temp)

    messageLabel = Label(canvas, text = "Notice:")
    messageLabel.place(x = 30, y = 60)
    messageLabel.configure(bg = "grey90", fg = "royalblue", font=("Arial Nova", 25))    

    updatedMessageLabel = Label(canvas, textvariable = message)
    updatedMessageLabel.place(x = 30, y = 120)
    updatedMessageLabel.configure(bg = "grey90", fg = "royalblue", font=("Arial Nova", 16))

1 个答案:

答案 0 :(得分:0)

您可以使用textwrap模块。

from tkinter import *
import textwrap

root = Tk()
text = "This is a very long sentence that needs wrapping else it will run out of space"
Label(root,text=textwrap.fill(text,20)).pack()

root.mainloop()
相关问题