def syntax_hilight_(event):
for kw in keyword.kwlist:
size = IntVar()
begin = self.txt_field.search(kw, "1.0", stopindex = "end", count = size)
#end_index = "%s + %sc" % (begin, size.get())
print(size.get())
size_int = float(size.get())
while size_int > 1:
size_int /= 10
float(begin)
end_index = float(begin) + size_int
self.txt_field.tag_add("search", float(begin), float(end_index))
self.txt_field.tag_config("search", foreground = "green")
当我运行此功能(按键)时,我收到此错误:
File "C:/Users/roman_000/PycharmProjects/neuron/ide_custom.py", line 36,
in syntax_hilight_
float(begin)
ValueError: could not convert string to float:
begin
是一个包含6.0,6.9,14.19等索引的字符串。我认为将这些字符串转换为float没有问题
答案 0 :(得分:2)
如果在文本中找不到kw
,则会出现此问题,因此begin
将为空,从而导致ValueError
。当begin
不为空时,请尝试突出显示。以下是我建议的解决方案:
def syntax_hilight_(self):
size = tk.IntVar()
for kw in keyword.kwlist:
begin = self.txt_field.search(kw, '1.0', stopindex='end', count=size)
if begin:
end_index = '%s + %sc' % (begin, size.get())
self.txt_field.tag_add('search', begin, end_index)
self.txt_field.tag_config('search', foreground='green')
答案 1 :(得分:0)
使用
try: float(begin)
except ValueError: raise RuntimeError(('could not convert to float', begin))