Avoid hard crash when error happens in tktextext event,
"""Extensions for tk.Text"""
import sys
import time
+import tkinter
import tkinter as tk
-from logging import exception, getLogger
+from logging import getLogger
from tkinter import TclError
from tkinter import font as tkfont
from tkinter import ttk
@@ -65,13 +66,24 @@ class TweakableText(tk.Text):
):
pass
else:
- exception(
+ logger.exception(
"[_dispatch_tk_operation] operation: " + operation + ", args:" + repr(args)
)
# traceback.print_exc()
return "" # Taken from idlelib.WidgetRedirector
+ except Exception as e:
+ # Need to catch to avoid the crash?
+ logger.exception("Exception in _dispatch_tk_operation")
+ from tkinter import messagebox
+
+ messagebox.showerror(
+ "Internal error",
+ "Error in _dispatch_tk_operation\n" + str(e),
+ parent=tkinter._default_root,
+ )
+
def set_read_only(self, value):
self._read_only = value
@@ -1113,7 +1125,7 @@ class EnhancedTextFrame(TextFrame):
): # In Python 3.6 you can use tk.EventType.ButtonPress instead of "4"
self.text.tag_remove("sel", "1.0", "end")
except tk.TclError:
- exception("on_gutter_click")
+ logger.exception("on_gutter_click")
def on_gutter_double_click(self, event=None):
try:
@@ -1121,7 +1133,7 @@ class EnhancedTextFrame(TextFrame):
self.text.tag_remove("sel", "1.0", "end")
self._gutter.tag_remove("sel", "1.0", "end")
except tk.TclError:
- exception("on_gutter_click")
+ logger.exception("on_gutter_click")
def on_gutter_motion(self, event=None):
try:
@@ -1135,7 +1147,7 @@ class EnhancedTextFrame(TextFrame):
self.text.mark_set("insert", "%s.0" % linepos)
self.text.focus_set()
except tk.TclError:
- exception("on_gutter_motion")
+ logger.exception("on_gutter_motion")
def _vertical_scrollbar_update(self, *args):
if not hasattr(self, "_vbar"):
