如何使用Python创建一个输入框?

11
我想创建一个屏幕上的输入框,供用户交互。
用户将看到一个带有输入字段的窗口,他们可以使用鼠标单击该字段。用户可以在字段中输入或删除文本,然后一旦完成添加文本,就可以按下OK按钮。最后,我的程序将存储此文本以供以后使用。
如何在Python中创建允许用户输入的文本框?

4个回答

17

你可以尝试使用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

当然,你可能想要阅读一个 Tkinter 教程。


6
我认为他想要一个简单的例子,比如 myText=tkSimpleDialog.askstring("Title","Enter a string:")。这是最简单的解决方案,但我从未使用过它。 - Uncle Dino
1
@Sasszem 您建议的代码出现了NameError: name 'tkSimpleDialog' is not defined错误。 - Stevoisiak
3
你需要先导入它,使用 import tkSimpleDialog - Uncle Dino

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

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

在实际应用中

以下是我制作的一个版本,因为在实际应用中,您需要在主窗口/表单上显示一个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()

-1

在IDLE中,您已经可以要求用户输入,就像这里我已经做了一个数学方程需要解决...

answer = int(input('What is the answer to 1+2?'))
def equation(answer):
    if answer == 3:
        print ('Correct!')
    else:
        print ('Wrong, it is 3!')

然后调用该函数...

equation(answer)

1
问题是:“屏幕输入框” 你的建议是针对控制台,而不是GUI。 - rundekugel

-4
最简单的方法是将输入设置为变量以供稍后使用,然后在程序中稍后调用该变量。
variable = str(input("Type into the text box."))

不需要str。输入已经返回了一个字符串。 - CozyCode

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接