首页 > 解决方案 > 如何在使用可变大小的标签中分隔文本

问题描述

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

我怎样才能让我的程序分隔文本,以便它只能走这么远,然后消息在下一行继续 图像显示了预期消息的一部分

    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))

标签: tkintertkinter-canvas

解决方案


您可以使用该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()

推荐阅读