lzb1001 发表于 2022-5-24 14:07:43

text组件的tags用法中使用search()方法查找内容

本帖最后由 lzb1001 于 2022-5-24 14:15 编辑

from tkinter import *



root = Tk()


text = Text(root, width=30, height=5)
text.pack()



text.insert(INSERT, 'I love FishC.com!')


def getIndex(text, index):
    return tuple(map(int, str.split(text.index(index), '.')))


start = '1.0'


while True:
    pos = text.search('o', start, stopindex=END)
    print(pos)
    if not pos:# ???
      break
    print('找到啦,o的位置在(行,列):', getIndex(text, pos))
    start = pos + '+1c'




root.title('Text组件Tags用法教学示例')


mainloop()

-----------------------------------------------
Windows 10 专业版 | Python 3.7.6
-----------------------------------------------

【我的问题】

红色部分的代码看不懂呢,请大神帮忙解释理解下?另外tuple这样是不是作为一个函数使用?

感谢大神不吝赐教,为新手解疑释惑。

赠人玫瑰,手有余香,好人一生平安!

来自星星的小明 发表于 2022-5-24 17:53:40

tuple(iterable)将可迭代系列转换为元组
map(function, iterable) 会根据提供的函数对指定序列做映射
不知道你是哪里不懂

lzb1001 发表于 2022-5-24 18:38:32

来自星星的小明 发表于 2022-5-24 17:53
tuple(iterable)将可迭代系列转换为元组
map(function, iterable) 会根据提供的函数对指定序列做映射
...

感觉好多行代码合成一行代码(这样不知道能不能称之为“”函数嵌套使用”)有时对初学者而言不好理解。

另外:

print('找到啦,o的位置在(行,列):', getIndex(text, pos))如果写成print('找到啦,o的位置在(行,列):', pos)好像也可以,只是返回结果的形式不太一样,见下:

---用print('找到啦,o的位置在(行,列):', getIndex(text, pos))返回如下:
1.3
找到啦,o的位置在(行,列): (1, 3)
1.14
找到啦,o的位置在(行,列): (1, 14)


---用print('找到啦,o的位置在(行,列):', pos)返回如下:
1.3
找到啦,o的位置在(行,列): 1.3
1.14
找到啦,o的位置在(行,列): 1.14
页: [1]
查看完整版本: text组件的tags用法中使用search()方法查找内容