如何在Listbox中包装

时间:2013-07-03 01:33:25

标签: python

from Tkinter import *

root = Tk()
root.title("Help")

scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)

help_message = 'This is the help menu. Please scroll through the menu to find the answer to your question'

listbox = Listbox(root)
listbox.pack()
listbox.insert(END, help_message)

listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)

mainloop()

当我运行此代码时,文本将超出列表框的边界。是否有一个参数,我可以添加到这个,以便我可以将文本包装到下一行。如果一个单词的一部分被切断,我不在乎。我试图让列表框更大但文本仍然没有包装。

由于

1 个答案:

答案 0 :(得分:0)

使用Text小部件而不是ListboxText小部件有wrap选项。 (无,CHAR,WORD)

from Tkinter import *

root = Tk()
root.title("Help")

scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)

help_message = 'This is the help menu. Please scroll through the menu to find the answer to your question'

txt = Text(root, wrap=WORD) # wrap=CHAR, wrap=NONE
txt.pack(expand=1, fill=BOTH)
txt.insert(END, help_message)

txt.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=txt.yview)

mainloop()