如何使用 Python 创建输入框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20865010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:25:36  来源:igfitidea点击:

How do I create an input box with Python?

pythonpython-3.xuser-inputprompt

提问by Lev

I want to create an on-screen input box that a user can interact with.

我想创建一个用户可以与之交互的屏幕输入框。

The user would see a window with an input field they can click with the mouse. The user could type or erase text in the field, then press OK once they have finished adding text. Lastly, my program would store this text for later use.

用户将看到一个带有输入字段的窗口,他们可以用鼠标单击。用户可以在字段中键入或删除文本,然后在完成添加文本后按 OK。最后,我的程序将存储此文本以备后用。

How can I create a text box in Python which allows user input?

如何在 Python 中创建允许用户输入的文本框?

采纳答案by Christian

You could try the Tkintermodule:

你可以试试Tkinter模块:

from tkinter import *

master = Tk()
e = Entry(master)
e.pack()

e.focus_set()

def callback():
    print e.get() # This is the text you may want to use later

b = Button(master, text = "OK", width = 10, command = callback)
b.pack()

mainloop()

Result:

结果:

Result

结果

Of course, you may want to read a Tkintertutorial.

当然,您可能想阅读Tkinter教程。

回答by TBrom

The simplest way to do it is to set an input equal to a variable for later use, and then call the variable later in the program.

最简单的方法是将输入设置为等于变量以供以后使用,然后在程序中稍后调用该变量。

variable = str(input("Type into the text box."))

回答by Jeremy Thompson

The Tk library actually has a function that does this although it is nominally 'private'. You can use it as follows.

Tk 库实际上有一个功能可以做到这一点,尽管它名义上是“私有的”。您可以按如下方式使用它。

import tkinter as tk

root = tk.Tk()
root.wm_geometry("800x600")
dialog = tk.Toplevel(root)
root_name = root.winfo_pathname(root.winfo_id())
dialog_name = dialog.winfo_pathname(dialog.winfo_id())
root.tk.eval('tk::PlaceWindow {0} widget {1}'.format(dialog_name, root_name))
root.mainloop()

This will place your dialog centred over the specified window (in this case the root window). Ref.

这会将您的对话框置于指定窗口(在本例中为根窗口)的中央。参考

In a real life application

在现实生活应用中

Here's a version I made because in real life you'll need to show an InputBox over the top of the Main Window/Form, and typically in a Modal (can't click on other windows) state, and then close it when the user clicks OK:

这是我制作的一个版本,因为在现实生活中,您需要在主窗口/窗体的顶部显示一个 InputBox,并且通常处于模态(不能单击其他窗口)状态,然后在用户关闭它时点击确定:

try:
    # for Python2
    import Tkinter as tk
except ImportError:
    # for Python3
    import tkinter as tk

class App:
    def __init__(self):
        self.HEIGHT = 700
        self.WIDTH = 800    
        root = tk.Tk()
        root.width = self.WIDTH
        root.height = self.HEIGHT
        self.dialogroot = root
        self.strDialogResult = ""    
        self.canvas = tk.Canvas(root, height=self.HEIGHT, width=self.WIDTH)
        self.canvas.pack()    
        frame = tk.Frame(root, bg='#42c2f4')
        frame.place(relx=0.5, rely=0.02, relwidth=0.96, relheight=0.95, anchor='n')  
        # Here is the button call to the InputBox() function
        buttonInputBox = tk.Button(frame, text="Input Box", bg='#cccccc', font=60, 
        command=lambda: self.InputBox())    
        buttonInputBox.place(relx=0.05, rely=0.1, relwidth=0.90, relheight=0.8)    
        root.mainloop()

    def InputBox(self):        
        dialog = tk.Toplevel(self.dialogroot)
        dialog.width = 600
        dialog.height = 100

        frame = tk.Frame(dialog,  bg='#42c2f4', bd=5)
        frame.place(relwidth=1, relheight=1)

        entry = tk.Entry(frame, font=40)
        entry.place(relwidth=0.65, rely=0.02, relheight=0.96)
        entry.focus_set()

        submit = tk.Button(frame, text='OK', font=16, command=lambda: self.DialogResult(entry.get()))
        submit.place(relx=0.7, rely=0.02, relheight=0.96, relwidth=0.3)

        root_name = self.dialogroot.winfo_pathname(self.dialogroot.winfo_id())
        dialog_name = dialog.winfo_pathname(dialog.winfo_id())

        # These two lines show a modal dialog
        self.dialogroot.tk.eval('tk::PlaceWindow {0} widget {1}'.format(dialog_name, root_name))
        self.dialogroot.mainloop()

        #This line destroys the modal dialog after the user exits/accepts it
        dialog.destroy()

        #Print and return the inputbox result
        print(self.strDialogResult)
        return self.strDialogResult

    def DialogResult(self, result):
        self.strDialogResult = result
        #This line quits from the MODAL STATE but doesn't close or destroy the modal dialog
        self.dialogroot.quit()


# Launch ...
if __name__ == '__main__':
    app = App()