如何在tkinter中使文本不超出列表框的边界?

问题描述 投票:0回答:1

我正在创建一个简单的程序,在tkinter的列表框中显示文本。有时我要显示的文本比列表框大,因此它不在屏幕上。我想知道是否有办法让文本在屏幕下方开始新行而不是离开屏幕。

代码

from tkinter import *
root = Tk()

message = "This message is too long to display on the listbox. I hope. 
that someone will help me find a solution to this problem."

listBox = Listbox(root, width = 50, height=15, bg="#2C2F33", fg="white")
listBox.grid(row=0, column=0, padx=10)

listBox.insert(END, message)
listBox.see(END)

root.mainloop()

问题:

The text goes off the screen.

预期结果:

Intended result.

非常感谢所有帮助!

python-3.x tkinter listbox
1个回答
0
投票

Listbox小部件是一个菜单项,可供选择。列表框项目不可能分布在多行或多行中。

您应该使用另一个名为Text的小部件

from tkinter import *

root = Tk()
message = "This message is too long to display on the listbox. I hope that someone will help me find a solution to this problem."
text = Text(root, width = 50, height=15, bg="#2C2F33", fg="white",wrap=WORD)

text.insert(INSERT, message)

text.grid(row=0, column=0, padx=10)

root.mainloop()

注意:wrap:此选项控制显示过宽的行。设置wrap = WORD,它将在最后一个单词之后换行会适合的。使用默认行为,wrap = CHAR,太长会损坏任何字符。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.