I am trying to prompt the user to enter a string of text. Is there available with python tkinter a Javascript like prompt?
6 Answers
Yes, use tkSimpleDialog.askstring:
tkSimpleDialog.askstring(title, prompt [,options])
Unfortunately this isn't in the main Python docs, so it's a bit hard to find.
-
7For Python 3:
from tkinter import simpledialog; answer = simpledialog.askstring(title, prompt)
– b_cSep 6, 2017 at 17:51 -
3@b_c is right, keep in mind that
prompt
stand forTk()
instance in his example.– JoDevJan 31, 2018 at 13:08 -
2When providing a link only answer it is best to also include the relevant information from the linked site in the case that the link breaks as that happens often. Jul 1, 2020 at 13:08
-
NB: For Python 3.x it's
tkinter.simpledialog.askstring(title, prompt, **kw)
. See the official documentation– wovanoJul 18, 2022 at 18:39
One of those situations where I find it after the question has been posted and since I had trouble finding the answer I will keep the question up.
You can use a tkSimpleDialog.
-
Nicholas Riley has a better answer with a working link. I will probably delete this answer soon as it doesn't provide any additional information anymore.– MitMaroJan 11, 2015 at 16:08
-
When providing a link only answer it is best to also include the relevant information from the linked site in the eventual case that the link breaks as that happens often. In this case your link does not work. Jul 1, 2020 at 13:08
You can ask a yes/no question with
from tkinter import *
answer = tkinter.messagebox.askquestion
Getting User Input With Entry Widgets
When you need to get a little bit of text from a user, like a name or an email address, use an Entry widget. They display a small text box that the user can type some text into. Creating and styling an Entry widget works pretty much exactly like Label and Button widgets. For example, the following code creates a widget with a blue background, some yellow text, and a width of 50 text units:
entry = tk.Entry(fg="yellow", bg="blue", width=50)
The interesting bit about Entry widgets isn’t how to style them, though. It’s how to use them to get input from a user. There are three main operations that you can perform with Entry widgets:
- Retrieving text with .get()
- Deleting text with .delete()
- Inserting text with .insert()
The best way to get an understanding of Entry widgets is to create one and interact with it. Open up a Python shell and follow along with the examples in this section. First, import tkinter and create a new window:
>>> import tkinter as tk
>>> window = tk.Tk()
Now create a Label and an Entry widget:
>>> label = tk.Label(text="Name")
>>> entry = tk.Entry()
The Label describes what sort of text should go in the Entry widget. It doesn’t enforce any sort of requirements on the Entry, but it tells the user what your program expects them to put there. You need to .pack() the widgets into the window so that they’re visible:
>>> label.pack()
>>> entry.pack()
Here’s what that looks like: Preview
I think this is what you might want
for python 2.7:
from Tkinter import *
root = Tk()
E = Entry(root)
E.pack()
root.mainloop()
for python 3:
from tkinter import *
root = Tk()
e = Entry(root)
e.pack()
root.mainloop()