python-3.x - 如何在 tkinter 的文本小部件中将文本输入到换行符?
问题描述
我正在创建一个简单的程序,在 tkinter 的列表框中显示文本。有时我想显示的文本比列表框大,所以它会离开屏幕。我想知道是否有办法让文本在下面开始一个新行而不是离开屏幕。
代码
from tkinter import *
root = Tk()
text = Text(root, width = 50, height=15, bg="#2C2F33", fg="white",wrap=WORD)
text.grid(row=0, column=0, padx=10)
while True:
message = input("Enter a message: ")
text.insert(INSERT, message)
root.mainloop()
问题:
预期结果
非常感谢所有帮助!
解决方案
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 时,任何过长的行都会在任何字符处中断。
推荐阅读
- javascript - Script error : "Uncaught SyntaxError: Unexpected identifier"
- c# - Update values in ConcurrentDictionary
> - c# - MSMQ 触发器,它们与使用 BeginPeek 和 EndPeek 有什么不同。C#
- qt - 如何使用 QAbstractTableModel 在 QML 中设置 TableViewColumn 的模型列索引
- java - 如何从最近的任务中删除应用程序或活动
- sql - SQL Join 三表 17
- git - vNext 构建管道以将远程 git 同步到本地存储库
- google-cloud-automl - 从 PredictionClient 获取检测到的对象的集合
- react-data-grid - 如何以编程方式设置单元格中文本的颜色
- vba - VBA循环宏不显示错误,但启动后不显示结果