Home > Mobile >  Python tkinter simpledialog
Python tkinter simpledialog

Time:02-24

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.

CodePudding user response:

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