|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lzb1001 于 2022-5-21 22:42 编辑
Windows 10(专业版) | Python 3.7.6
----------------------------------
from tkinter import *
import time
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:
x = input('请输入想要查找的字母:')
pos = text.search(x, start, stopindex=END)
if not pos:# ???
print('找不到%s哦,请确认后重新输入……' % x)
print('找到啦,%s的位置在(行,列):'% x, getIndex(text, pos))
start = pos + '+1c'
time.sleep(1)
root.title('Text组件Tags用法教学示例')
mainloop()
-----------------------------
运行后搜索不存在的字母如W时,返回如下错误:
请输入想要查找的字母:W
找不到W哦,请确认后重新输入……
Traceback (most recent call last):
File "D:\work\p15_29.py", line 36, in <module>
print('找到啦,%s的位置在(行,列):'% x, getIndex(text, pos))
File "D:\work\p15_29.py", line 25, in getIndex
return tuple(map(int, str.split(text.index(index), '.')))
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3268, in index
return str(self.tk.call(self._w, 'index', index))
_tkinter.TclError: bad text index ""
【我的问题】
问题出在哪里?该如何修改?
我的本意是:当输入不存在的字母时,提示不存在,同时跳出让用户重新输入 |
|