【发布时间】:2014-04-29 22:43:30
【问题描述】:
您知道如何在 Tkinter 中执行以下操作:
self.scrolled_text.replace("insert-1c", INSERT, myText);
它会用myText替换当前位置左边的字符吗?
那么,您可以在哪里记录所有可以代替“insert-1c”的内容?
我知道 -1c 意味着减去一个字符。你可以用 -2c 表示负二,或 +5c 表示加 5 个字符。我之所以知道这一点,是因为我在 Google 上花了一些时间后检查了某人的代码(不是因为我找到了文档)。
还有更多要知道的吗,还是这样?无论如何,我希望能够在不进行搜索和查找空白的情况下使用单词而不是字符。他们应该有这样的文档 wiki。
编辑:实际上,我为 Tkinter 找到了不止一个 wiki,其中一个有点讽刺地讨论了这个问题。也有一个官方的 python.org (虽然我不知道是否其中任何一个是讨论这个特定主题的那个):
编辑:既然我已经得到了答案,我将分享一些我学到的东西,这些东西可能会对某些人有所帮助。如果您想进行比较,ScrolledText.get(index1, index2) 非常重要。 ScrolledText.index(INSERT) 将返回 INSERT 的行和列,正如 Bryan Oakley 在他的回答中用不同的词所说的那样(例如,"1.0" 表示第 1 行、第 0 列或文件的开头)。
EDIT2:ScrolledText.compare(index1, "==", index2) 使用更少的代码进行比较,正如人们想象的那样。我当时不知道这件事。
这是我编写的代码,用于模拟我喜欢的控制退格和控制删除功能。
Control-delete(self.cur_scroll() 是我的 ScrolledText 小部件;" " 包含一个空格和一个不间断空格):
insert=self.cur_scroll().get(INSERT);
if insert in " " and insert!="":
while insert in " " and insert!="":
self.cur_scroll().delete(INSERT);
insert=self.cur_scroll().get(INSERT);
elif insert not in "\n\t .?!,@#…¿/\\\"'—–":
while insert not in "\n\t .?!,@#…¿/\\\"'—–":
self.cur_scroll().delete(INSERT);
insert=self.cur_scroll().get(INSERT);
if insert in " ":
while insert in " " and insert!="":
self.cur_scroll().delete(INSERT);
insert=self.cur_scroll().get(INSERT);
else:
if insert in "\n\t" and insert!="":
if self.cur_scroll().get(INSERT, END).strip()=="" and self.cur_scroll().index(INSERT)!=self.cur_scroll().index(END):
self.cur_scroll().delete(INSERT, END);
else:
while insert in "\n\t " and insert!="":
self.cur_scroll().delete(INSERT);
insert=self.cur_scroll().get(INSERT);
else:
self.cur_scroll().delete(INSERT);
控制退格:
if self.cur_scroll().index("insert-1c")!="1.0" and self.cur_scroll().index(INSERT)!="1.0":
if self.cur_scroll().index("insert wordstart-1c")=="1.0":
i=0;
while self.cur_scroll().index(INSERT)!="1.0" and i<25:
self.cur_scroll().delete("insert-1c");
i+=1;
else:
insertm1c=self.cur_scroll().get("insert-1c");
if insertm1c in "\n\t.?!,@#…¿/\\\"'—–" and insertm1c!="":
i=0;
while (insertm1c in "\n\t.?!,@#…¿/\\\"'—–" and insertm1c!="") and i<25:
self.cur_scroll().delete("insert-1c");
insertm1c=self.cur_scroll().get("insert-1c");
i+=1;
else:
while insertm1c not in "\n\t .?!,@#…¿/\\\"'—–" and insertm1c!="":
self.cur_scroll().delete("insert-1c");
insertm1c=self.cur_scroll().get("insert-1c");
while insertm1c in " " and insertm1c!="":
self.cur_scroll().delete("insert-1c");
insertm1c=self.cur_scroll().get("insert-1c");
【问题讨论】:
标签: python python-3.x tkinter