Search code examples
pythontkinterattributesspyderlinux-mint

Python tkinter works in spyder but missing attribute (simpledialog) in shell


I created a simple python script to open a simple dialog box. It works in spyder but not when I run the commands in the shell.

I'm running Linux Mint. I installed Spyder with Anaconda's miniconda.

Here's my code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tkinter as tk
tkwin = tk.Tk()
tkwin.withdraw()
mytext = tk.simpledialog.askstring("A Simple Dialog Box", "Enter some text:", parent=tkwin)

In Spyder, I get the dialog box. When running in the shell, I run python and enter the same Python commands, but I get an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'tkinter' has no attribute 'simpledialog'

I confirmed that I'm running the same Python in Spyder and script.

Interestingly, print(dir(tk)) in spyder shows simpledialog, but print(dir(tk)) in the shell does not. If I type tk in Spyder's Console, I get

<module 'tkinter' from '/home/keith/Apps/miniconda3/lib/python3.12/tkinter/__init__.py'>

In the shell I get the same thing. What's going on and how do I fix it? Thanks, Keith


Solution

  • You are missing tkinter.simpledialog module.

    Snippet:

    import tkinter as tk
    import tkinter.simpledialog #<== Add to module
    tkwin = tk.Tk()
    tkwin.withdraw()
    mytext = tk.simpledialog.askstring("A Simple Dialog Box", "Enter some text:", parent=tkwin)