gpt4 book ai didi

python - 文本不包含任何标记为 "sel"tkinter 的字符

转载 作者:行者123 更新时间:2023-12-05 03:46:34 26 4
gpt4 key购买 nike

我最近阅读了由受人尊敬的 Bryan Oakley( Tkinter adding line number to text widget ) 撰写的答案,他在其中展示了有关解决问题的示例代码。

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

_tkinter.TclError: 文本不包含任何用“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):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)

# create a proxy for the underlying widget
self._orig = self._w + "_orig"
self.tk.call("rename", self._w, self._orig)
self.tk.createcommand(self._w, self._proxy)

def _proxy(self, *args):
# let the actual widget perform the requested action
cmd = (self._orig,) + args
result = self.tk.call(cmd)

# generate an event if something was added or deleted,
# or the cursor position changed
if (args[0] in ("insert", "replace", "delete") or
args[0:3] == ("mark", "set", "insert") or
args[0:2] == ("xview", "moveto") or
args[0:2] == ("xview", "scroll") or
args[0:2] == ("yview", "moveto") or
args[0:2] == ("yview", "scroll")
):
self.event_generate("<<Change>>", when="tail")

# return what the actual widget returned
return result

class Example(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.text = CustomText(self)
self.vsb = tk.Scrollbar(orient="vertical", command=self.text.yview)
self.text.configure(yscrollcommand=self.vsb.set)
self.text.tag_configure("bigfont", font=("Helvetica", "24", "bold"))
self.linenumbers = TextLineNumbers(self, width=30)
self.linenumbers.attach(self.text)

self.vsb.pack(side="right", fill="y")
self.linenumbers.pack(side="left", fill="y")
self.text.pack(side="right", fill="both", expand=True)

self.text.bind("<<Change>>", self._on_change)
self.text.bind("<Configure>", self._on_change)

self.text.insert("end", "one\ntwo\nthree\n")
self.text.insert("end", "four\n")
self.text.insert("end", "five\n")

def _on_change(self, event):
self.linenumbers.redraw()

if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()

最佳答案

问题是当您尝试复制或删除未选择的文本时会出错。解决方案是添加一些额外的测试。将您的 _proxy 函数替换为我的函数以获得有效结果。

def _proxy(self, command, *args):
# avoid error when copying
if command == 'get' and (args[0] == 'sel.first' and args[1] == 'sel.last') and not textArea.tag_ranges('sel'): return

# avoid error when deleting
if command == 'delete' and (args[0] == 'sel.first' and args[1] == 'sel.last') and not textArea.tag_ranges('sel'): return

cmd = (self._orig, command) + args
result = self.tk.call(cmd)

if command in ('insert', 'delete', 'replace'):
self.event_generate('<<TextModified>>')

return result

关于python - 文本不包含任何标记为 "sel"tkinter 的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65228477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com