【发布时间】:2021-03-15 18:44:16
【问题描述】:
有什么方法可以去除 tkinter 中的重复行吗?
代码如下:
from tkinter import *
root = Tk()
def remove_duplicate():
# Code to remove all duplicate lines in the text widget
pass
text = Text(root , width = 65, height = 20, font = "consolas 14")
text.pack()
text.insert('1.0' , '''Hello world\n\nHello world\n\nBye bye\n\n\n\n\nBye bye\nBye bye''')
remove_button = Button(root , text = "Remove Duplicate Lines" , command = remove_duplicate)
remove_button.pack()
mainloop()
当我点击remove_button 时,我希望删除文本小部件中的所有重复行。
在这种情况下,我有字符串:
"""
Hello world
Hello world
Bye bye
Bye bye
Bye bye
"""
,所以当我删除重复的行时,我应该得到类似的东西:
"""
Hello world
Bye bye
"""
有没有办法在 tkinter 中实现这一点?
如果有人能帮助我,那就太好了。
【问题讨论】:
-
获取文本作为字符串,对其进行操作(this 可能有用),然后将其放回
<tk.Text> -
stackoverflow 上有很多关于从列表中删除重复项的问题,并且文本小部件实际上是一个字符串列表。您是否研究过如何从字符串列表中删除重复项?
-
@BryanOakley:是的,我尽我所能,但没有成功。
标签: python tkinter duplicates