Python Tkinter Prompt
I am trying to prompt the user to enter a string of text. Is there availabl开发者_开发百科e with python tkinter a Javascript like prompt?
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.
try this:
import tkinter
x = tkinter.tkSimpleDialog.askstring
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.
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()
精彩评论