文本小部件中的文本作为变量 [英] Text in Text Widget as a variable

查看:28
本文介绍了文本小部件中的文本作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个带有滚动条的小文本小部件,我有一个问题.如何使此 Text 小部件中的文本成为变量?如果我将此文本设为变量,我将能够打开一个文本文件并编辑它的文本或保存我编写的文本等,或者可能这是我处理此问题的错误方式,有没有更好的方法这个?

so I've got this little Text widget with a scroll bar and I've got a question. How do I make text in this Text widget a variable ? If I made this text a variable I would be able to open a text file and edit it's text or save the text I've written, etc or maybe it's a wrong way that I'm approaching this, is there a better way to do this ?

推荐答案

没有将变量与文本小部件关联的选项.您可以通过使用变量跟踪和小部件绑定来实现相同的目的,但很少值得付出努力.

There is no option to associate a variable with a text widget. You can achieve the same thing by using variable traces and widget bindings but it's rarely worth the effort.

与文本小部件交互的典型方式是将文件读入变量,然后使用小部件的insert 方法将文本放入小部件中.然后,为了节省您的时间,只需执行相反的操作——使用 get 方法从小部件获取文本,并将数据写入文件.

The typical way to interact with the text widget is to read a file into a variable then use the insert method of the widget to put the text into the widget. Then, to save you just do the reverse -- get the text from the widget with the get method, and write the data to a file.

一个提示:当您执行 get 时,不要将文本从 1.0 获取到end",而是使用end-1c".如果您将end"指定为最后一个字符,您将获得 tk 始终添加的隐式换行符,这意味着每次执行加载/保存循环时,您的文本文件都会增加一个字符.

One tip: when you do a get, don't get the text from 1.0 to "end", use "end-1c" instead. If you specify "end" as the last character you'll get the implicit newline that tk always adds, meaning your text file will grow by one character each time you do a load/save cycle.

这篇关于文本小部件中的文本作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆