如何让文本输入到 tkinter 文本小部件中的换行符?
How to get text to input to a newline in a text widget in tkinter?
我正在创建一个简单的程序,它在 tkinter 的列表框中显示文本。有时我想要显示的文本比列表框大,所以它会离开屏幕。我想知道是否有办法让文本在下面开始一个新行而不是离开屏幕。
Code
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()
Problem:
Intended result
非常感谢所有帮助!
列表框小部件是一个可供选择的项目菜单。列表框项不可能分布在多行或一行中。
您应该使用另一个名为 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()
note:
wrap : This option controls the display of lines that are too wide. Set wrap=WORD and it will break the line after the last word
that will fit. With the default behavior, wrap=CHAR, any line that
gets too long will be broken at any character.
我正在创建一个简单的程序,它在 tkinter 的列表框中显示文本。有时我想要显示的文本比列表框大,所以它会离开屏幕。我想知道是否有办法让文本在下面开始一个新行而不是离开屏幕。
Code
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()
Problem:
Intended result
非常感谢所有帮助!
列表框小部件是一个可供选择的项目菜单。列表框项不可能分布在多行或一行中。
您应该使用另一个名为 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()
note:
wrap : This option controls the display of lines that are too wide. Set wrap=WORD and it will break the line after the last word that will fit. With the default behavior, wrap=CHAR, any line that gets too long will be broken at any character.