MotoCyclotron
MotoCyclotron

Reputation: 3

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.

Upvotes: 0

Views: 4650

Answers (2)

Eshaan Buddhisagar
Eshaan Buddhisagar

Reputation: 11

from tkinter import simpledialog

You imported simple dialog to your module, which means you don't need the prefix:

tkinter.

Since you have the simple dialog installed. You can do this:

from tkinter import simpledialog
input = simpledialog.askstring("Title of Window", "Question")
# You don't need the Tk window if you don't want it; simple dialog will create a second window to ask the question.

Upvotes: 1

Daweo
Daweo

Reputation: 36390

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

Upvotes: 4

Related Questions