Python tkinter simpledialog

2.2k views Asked by At

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.

1

There are 1 answers

1
Daweo On BEST ANSWER

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()