'How to fix bad window path name error in simpledialog tkinter?
Considering a basic app which is a class inheriting from tk.Tk, I get an unexpected error message when using the simpledialog module from tkinter. It is related to overriding the __str__ method in this class as the simpledialog does work without the string override, so my guess would be the simpledialog expects default behavior there. See the error message below:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\**\Python39\lib\tkinter\__init__.py", line 1884, in __call__
return self.func(*args)
File "C:\**\bad_window_error.py", line 22, in start_dialog
result = simpledialog.askstring('Simple dialog', 'Fill in a string..')
File "C:\**\Python39\lib\tkinter\simpledialog.py", line 399, in askstring
d = _QueryString(title, prompt, **kw)
File "C:\**\Python39\lib\tkinter\simpledialog.py", line 376, in __init__
_QueryDialog.__init__(self, *args, **kw)
File "C:**\Python39\lib\tkinter\simpledialog.py", line 271, in __init__
Dialog.__init__(self, parent, title)
File "C:\**\Python39\lib\tkinter\simpledialog.py", line 138, in __init__
self.transient(parent)
File "C:\**\Python39\lib\tkinter\__init__.py", line 2225, in wm_transient
return self.tk.call('wm', 'transient', self._w, master)
_tkinter.TclError: bad window path name "random"
I have put together a minimal example, see below. The actual code I am working on is much bigger with many dependencies and so far I have only noticed this error occurring with the simpledialog module.
import tkinter as tk
from tkinter import ttk
from tkinter import simpledialog, messagebox
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title('Random app')
self.option_add('*tearOff', False)
self.start_dialog_button = ttk.Button(self, text='Run simple dialog', command=self.start_dialog)
self.start_dialog_button.grid(row=0, column=0, sticky='nesw')
def __str__(self):
return "random"
@staticmethod
def start_dialog():
messagebox.showinfo('hello', 'this works')
result = simpledialog.askstring('Simple dialog', 'Fill in a string..')
messagebox.showinfo('Result', result)
if __name__ == '__main__':
app = App()
app.mainloop()
If the string override is commented out, the code should run as expected. Is there a way to use the simpledialog module while still overriding the default __str__ method of tk.Tk or should I never do that?
Solution 1:[1]
IT's better not to override the default functions ; If you're looking to do it to change the output results, it can easily be done without creating extra issues.
@staticmethod
def start_dialog():
messagebox.showinfo('hello', 'this works')
result = simpledialog.askstring('Simple dialog', 'Fill in a string..')
if (result is None or result == '') :
result="random"
else:
result=result+" this should work"
messagebox.showinfo('Result', result)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ran A |
