[英]How to retrieve lines from text widget individually
I am trying to retrieve each new line in a text widget separately, I have 3 instances of Container class, the first instance prints data as expected but for second instance duplicates of first line are returned 我正在尝试分别在文本小部件中检索每一行,我有3个Container类实例,第一个实例按预期方式打印数据,但对于第二个实例,则返回第一行的副本
I am using object.get('current linestart', 'current lineend') to return new lines separately 我正在使用object.get('current linestart','current lineend')分别返回新行
full code: https://pastebin.com/mLR3zbFg 完整代码: https : //pastebin.com/mLR3zbFg
class Container(tk.Frame):
def __init__(self, parent = None, priority = 3, bg = 'bisque'):
self.inpList = []
self.b = tk.Button(self.f, text = 'add', command = lambda: self.add_task(priority))
def add_task(self, priority): # refer 1.2_text for implementation
finished = False
self.t = tk.Text(self.f)
self.t.configure(height = 10, wrap = 'word')
self.t.bind("<Return>", self.save_task)
self.t.pack(fill = tk.X)
def print_all(self):
print(self.inpList)
def save_task(self, event):
td = self.t.get('current linestart', 'current lineend')
self.inpList.append(td)
if __name__ == '__main__':
window = tk.Tk()
window.minsize(300, 600)
p1 = Container(window, priority = 1)
p2 = Container(window, bg = 'blue', priority = 2)
p3 = Container(window, bg = 'red', priority = 3)
window.mainloop()
figbeam's answer could work, but it wouldn't be great if you had a lot of text inputted and it seems like you want to read each line individually. figbeam的答案可能有用,但是如果您输入了很多文本,并且似乎想要单独阅读每一行,那将不是很好。 Here's a better solution in my opinion:
我认为这是一个更好的解决方案:
According to the docs , current
doesn't seem to do what you expect; 根据文档 ,
current
似乎并没有达到您的预期。 namely, current will give you to the character closest to your mouse (and only if you actually move your mouse). 也就是说,电流会带您到最接近鼠标的字符(并且仅当您实际移动鼠标时)。 This could be a reason why you were noticing weird behavior for the widgets that were below but not the one on top.
这可能就是为什么您注意到下面的小部件而不是上面的小部件的奇怪行为的原因。
A better solution is to move to the end of the text, then up one line, and then use the linestart
and lineend
selectors you had prior. 更好的解决方案是移动到文本的末尾,然后向上移动一行,然后使用之前使用的
linestart
和lineend
选择器。 Namely change 即改变
td = self.t.get('current linestart', 'current lineend')
to 至
td = self.t.get('end - 1 lines linestart', 'end - 1 lines lineend')
Things should work as expected after this change! 进行此更改后,事情应该会按预期进行!
Is it necessary to read each line separately? 是否需要分别阅读每一行? Otherwise you could read the lines into a list and then work with the list items separately.
否则,您可以将这些行读入列表,然后分别处理列表项。
As far as I can tell the delimiter for lines is newline, which can make it look odd if you use wrap. 据我所知,行的定界符是换行符,如果使用自动换行,这会使它看起来很奇怪。
Here's an example of reading multiple lines into a list: 这是将多行读入列表的示例:
line_list = textbox.get('1.0', 'end').split('\n')
for line in line_list:
# Process line
Just a thought. 只是一个想法。
The line td = self.t.get('current linestart', 'current lineend')
doesn't work as expected. td = self.t.get('current linestart', 'current lineend')
无法正常工作。 One solution is to read the whole content of the text box at each update (as suggested also in https://stackoverflow.com/a/55739714/2314737 ). 一种解决方案是在每次更新时读取文本框的全部内容(如https://stackoverflow.com/a/55739714/2314737中所建议)。
In the code substitute the function save_task()
with: 在代码中,将函数
save_task()
替换为:
def save_task(self, event):
td = self.t.get("1.0",'end').rstrip()
self.inpList = td.split('\n')
This also takes care of any deleted lines that otherwise will stay in td
. 这也可以处理所有删除的行,否则这些行将保留在
td
。
See also this similar question: How to read the input(line by line) from a multiline Tkinter Textbox in Python? 另请参见类似的问题: 如何在Python中从多行Tkinter文本框中读取输入(逐行)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.