鱼C论坛

 找回密码
 立即注册
查看: 1212|回复: 2

哪里错了,请大神帮忙看看指点修改下

[复制链接]
发表于 2022-5-21 22:28:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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 ""



【我的问题】

问题出在哪里?该如何修改?

我的本意是:当输入不存在的字母时,提示不存在,同时跳出让用户重新输入
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-5-22 05:55:53 From FishC Mobile | 显示全部楼层
在找不到后面添加一个continue或break
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-22 11:07:57 | 显示全部楼层
本帖最后由 白two 于 2022-5-22 11:10 编辑

加一个 continue, 同时 start 改成 = 1.0(这个可能和编辑器有关, 你自己看着改, 反正我"1.0"是有bug的, 1.0正常运行)
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):
    print(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)
    print(pos)
    if not pos:
        print('找不到%s哦,请确认后重新输入……' % x)
        continue
    print('找到啦,%s的位置在(行,列):'% x, getIndex(text, pos))
    start = 1.0
    time.sleep(1)

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

mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-8 00:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表