10

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 6

15

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.

4
  • 7
    For Python 3: from tkinter import simpledialog; answer = simpledialog.askstring(title, prompt)
    – b_c
    Sep 6, 2017 at 17:51
  • 3
    @b_c is right, keep in mind that prompt stand for Tk() instance in his example.
    – JoDev
    Jan 31, 2018 at 13:08
  • 2
    When 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.
    – Mike - SMT
    Jul 1, 2020 at 13:08
  • NB: For Python 3.x it's tkinter.simpledialog.askstring(title, prompt, **kw). See the official documentation
    – wovano
    Jul 18, 2022 at 18:39
5

try this:

import tkinter
x = tkinter.tkSimpleDialog.askstring
1

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.

2
  • 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.
    – MitMaro
    Jan 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.
    – Mike - SMT
    Jul 1, 2020 at 13:08
1

You can ask a yes/no question with

from tkinter import *
answer = tkinter.messagebox.askquestion
0
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:

  1. Retrieving text with .get()
  2. Deleting text with .delete()
  3. 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

1
  • I could not reproduce the windows on my Mac (Monterey, M1)
    – Stefano
    Jul 22, 2022 at 22:58
-2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.