[Tkinter-discuss] tk bug in wait_visibility?

Vasilis Vlachoudis Vasilis.Vlachoudis at cern.ch
Fri Feb 14 08:47:45 EST 2020


In some dialogs I wanted to restore the size from the last time it was opened
therefore, before calling the wait_window I have

self.wait_visibility()
self.geometry(...restore...)     #<<< the geometry setting will not work if it is not visible
self.wait_window()

If something happens during the filling of the dialog information I show a messagebox.
The messagebox is triggering the event loop, which displays also the dialog
If the dialog is displayed then the execution gets stuck in the  "wait_visibility", 
and when the user closes the dialog, the execution proceeds to the wait_window
which causes an exception since the window do not longer exits.

I think it is a tk bug, that the wait_visibility waits despite the window is visible.
The only way to cure it is to add a check "if self.winfo_ismapped()"

Example:


import tkinter as tk
import tkinter.messagebox as messagebox

error = True

class Dialog(tk.Toplevel):
	width  = 320
	height = 240

	def __init__(self, master, *args):
		super().__init__(master, *args)
		self.transient(master)

		self.b = tk.Button(self, text="Close", command=self.close)
		self.b.pack()

		if error:
			messagebox.showerror("Error",
				"Something happened",
				parent=self)

		#if not self.winfo_ismapped(): self.wait_visibility()
		self.wait_visibility()
		if Dialog.width>0:
			self.geometry(f"{Dialog.width}x{Dialog.height}")
		self.wait_window()

	def close(self):
                # save size... and destroy...
		self.destroy()


root = tk.Tk()
dlg = Dialog(root)


More information about the Tkinter-discuss mailing list