小编Awe*_*Sam的帖子

文本不包含任何用“sel”标记的字符 tkinter

我最近阅读了受人尊敬的 Bryan Oakley( Tkinter 添加行号到文本小部件)撰写的答案,其中他展示了有关解决问题的示例代码。

当我尝试处理该代码时,它工作正常,直到我复制或粘贴某些内容,即按Ctrl+CCtrl+V。它关闭了 tkinter 窗口并出现以下错误:

_tkinter.TclError: text doesn't contain any characters tagged with "sel"

这是代码:

import tkinter as tk

class TextLineNumbers(tk.Canvas):
    def __init__(self, *args, **kwargs):
        tk.Canvas.__init__(self, *args, **kwargs)
        self.textwidget = None

    def attach(self, text_widget):
        self.textwidget = text_widget

    def redraw(self, *args):
        '''redraw line numbers'''
        self.delete("all")

        i = self.textwidget.index("@0,0")
        while True :
            dline= self.textwidget.dlineinfo(i)
            if dline is None: break
            y = dline[1]
            linenum = str(i).split(".")[0]
            self.create_text(2,y,anchor="nw", text=linenum)
            i = self.textwidget.index("%s+1line" % i)

class CustomText(tk.Text): …
Run Code Online (Sandbox Code Playgroud)

python tkinter python-3.x

4
推荐指数
1
解决办法
1020
查看次数

标签 统计

python ×1

python-3.x ×1

tkinter ×1