Search code examples
pythontkinternameerrorsimpledialog

Python tkinter simpledialog


I have a Python script which generates a GUI window with tkinter library. I'd like to make some of it's buttons display a prompt - small window to ask the user for some number (something like in JavaScript). I tried the following command:

x = tkinter.simpledialog.askstring

But it returns an error:

NameError: name 'tkinter' is not defined

and no prompt is generated, although I have imported the library in the script's beginning:

from tkinter import *
from tkinter import simpledialog

Other elements (buttons, labels etc.) in the main window work correctly. Please help.


Solution

  • askstring is part of tkinter.simpledialog so you might import it like so

    from tkinter.simpledialog import askstring
    

    usage example

    import tkinter as tk
    from tkinter.simpledialog import askstring
    root = tk.Tk()
    x = askstring("Title", "Prompt")
    print(x)
    root.mainloop()