[英]Is there an Alternative to textvariable on ttk.Entry widget, for tk.Text widget on TK inter?
so according to Tkinter documentation, the purpose of an Entry widget is to allow the user to enter or edit a single line of text .因此根据 Tkinter 文档,Entry 小部件的目的是允许用户输入或编辑单行文本。 I want to get enter and Edit multiple lines of text but there is a problem.
我想输入并编辑多行文本,但出现问题。 The alternative suggested by tkinter, Text, doesn't do the job at all!
tkinter 文本建议的替代方案根本无法完成工作!
here is my code using ttk.Entry:这是我使用 ttk.Entry 的代码:
def _set_project_description_frame(self):
project_description_frame = ttk.Frame(self._main_frame)
ttk.Label(project_description_frame, text="Project Description").pack(side=tk.TOP, anchor="nw")
ttk.Entry(project_description_frame, textvariable=self._project_description).pack(side=tk.LEFT,
expand=True, fill=tk.X)
return project_description_frame
This produces the following tkinter output:这会产生以下 tkinter output:
but what I really wan is to have the description section actually look like a place where a description would go, so I replaced ttk.Entry with tk.Text:但我真正想要的是让描述部分看起来像一个描述 go 的地方,所以我用 tk.Text 替换了 ttk.Entry:
def _set_project_description_frame(self):
project_description_frame = ttk.Frame(self._main_frame)
ttk.Label(project_description_frame, text="Project Description").pack(side=tk.TOP, anchor="nw")
tk.Text(project_description_frame, height=5).pack(side=tk.LEFT, expand=True, fill=tk.X)
return project_description_frame
The result shown in the picture bellow:结果如下图所示:
The last result is exactly what I want but the problem is that without the passing textvariable=self._project_description
to tk.Text
it's impossible to actually save whatever is typed into that text field.最后的结果正是我想要的,但问题是,如果不将
textvariable=self._project_description
传递给tk.Text
,就不可能实际保存输入到该文本字段中的任何内容。
So my question is, how do I fix the code in order to be able to capture what's input into the text field the same way ttk.Entry does when I can't pass textvariable=self._project_description
to it?所以我的问题是,当我无法将
textvariable=self._project_description
传递给它时,如何修复代码以便能够像 ttk.Entry 一样捕获输入到文本字段中的内容?
The Text
widget doesn't support textvariable
because a Text
widget can contain more than just text. Text
小部件不支持textvariable
,因为Text
小部件可以包含的不仅仅是文本。 To get the content out of the Text
widget you need to call the get
method on the widget.要从
Text
小部件中获取内容,您需要调用小部件上的get
方法。
To get all of the contents of the widget you first need to keep a reference to the widget.要获取小部件的所有内容,您首先需要保留对小部件的引用。 Then it's just a matter of calling
get
with an index for the range of text you want.然后,只需使用所需文本范围的索引调用
get
即可。 Typically the two indexes will be the strings "1.0"
for the first character, and "end-1c"
for the last character before the newline added by the text widget itself.通常这两个索引是第一个字符的字符串
"1.0"
,文本小部件本身添加的换行符之前的最后一个字符的"end-1c"
。
def _set_project_description_frame(self):
...
self.text_widget = tk.Text(project_description_frame, height=5)
self.text_widget.pack(side=tk.LEFT, expand=True, fill=tk.X)
...
With that, anywhere else in your code that needs to fetch the data can do this:有了它,代码中任何其他需要获取数据的地方都可以这样做:
self.text_widget.get("1.0", "end-1c")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.