Thank you Michael, >> grab_release() on a widget that has already been destroyed or, in the/
this is also my understanding, however the places that happens are on Dialog code(s) like import tkinter as tk class Dialog(tk.Toplevel): width = -1 height = -1 # ---------------------------------------------------------------------- def __init__(self, master, **kw): super().__init__(master, class_="MyDialog", **kw) self.transient(master) self.title("My title") frame = tk.Frame(self) frame.pack(side=tk.TOP, fill=tk.BOTH) # ... various widget+buttons creation #... #... self.protocol("WM_DELETE_WINDOW", self.close) if Dialog.width>0: self.geometry("%dx%d"%(Dialog.width, Dialog.height)) self.wait_visibility() self.focus_set() self.grab_set() self.wait_window() #----------------------------------------------------------------------- def close(self, event=None): Dialog.width = self.winfo_width() Dialog.height = self.winfo_height() self.grab_release() self.destroy() My impression is that on slow network connections (using remote X11 session), sometimes the dialog is displayed before the wait_visibility is being executed and if the user closes the window from the window manager, it arrives to the focus_set, grab_set or wait_window with a window that doesn't exist and then I get the error message. I've once got also the TclError on the "frame = tk.Frame(self)" command Maybe the self.geometry() is the culprit? This types of errors are not something frequent. They are rare, but they happen. We have several thousands active users around the world and I get an automatic report maybe once every a couple of weeks. Vasillis ________________________________________ From: Tkinter-discuss [tkinter-discuss-bounces+vasilis.vlachoudis=cern...@python.org] on behalf of Michael Lange [klappn...@web.de] Sent: Wednesday, March 31, 2021 10:10 To: tkinter-discuss@python.org Subject: Re: [Tkinter-discuss] Exception protection to tk calls Hi, On Wed, 31 Mar 2021 06:53:26 +0000 Vasilis Vlachoudis <vasilis.vlachou...@cern.ch> wrote: > Is there any recommendation which tkinter calls have to be protected > with an exception handling I occasionally receive traceback errors from > my users on various calls like: grab_current, grab_release, focus_get > most of the times I protect them with with a TclError handler catching > exceptions like_tkinter.TclError: bad window path name > > Also which type of exception has to be protected, this is the first > time I've got a KeyError from a tkinter call, which normally should be > innocent focus_get()" it is a bit hard to guess what's going on without a real code example. It seems like you get these errors when either calling for example grab_release() on a widget that has already been destroyed or, in the last example, calling focus_get() while a messagebox is being displayed (not sure how this can actually happen in real-world code, I was only able to reproduce that error message with a non-sensical example, like >>> def foo(): ... root.after(1000, lambda: print(root.focus_get())) ... messagebox.showinfo() ... ) . I know that this is not really an answer to your question, but my guess is that all of these situations may actually be caused by programming errors which, once they have been discovered, should rather be fixed properly than worked around with a try-except clause. For example, if the window on which grab_release() is called might no longer exist, something like if widget.winfo_exists(): widget.grab_release() might do the trick. If you want to use try-except protection anyway, I don't think that there is any general rule which methods should be protected with which exception type. I think the only way to find out is to examine the tkinter code and find out for yourself which exception(s) are likely to occur. For example with focus_get() I think the KeyError caused by nametowidget() is probably the only exception one might expect. Of course one could always play it safe with something like: try: focus = self.focus_get() except: focus = None Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Death, when unnecessary, is a tragic thing. -- Flint, "Requiem for Methuselah", stardate 5843.7 _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss