在python中向用户显示一条消息

时间:2015-07-25 03:57:47

标签: python text output

快速提问。是否有一种非常简单的方法可以在python中的文本框中向用户显示消息?我只需要事先输入一个字符串,这样我就可以在循环运行后显示消息。谢谢!

编辑:只是Windows 8.1和直接python 2.7

1 个答案:

答案 0 :(得分:1)

如果我想要一个窗口允许用户输入某些文字,然后将其显示为消息框,那么您需要两个模块tkMessageBoxTinker ,Python 2.7+中的两个标准模块。

以下文档代码执行上述操作

from Tkinter import *
import tkMessageBox

def getInfo():
    #This function gets the entry of the text area and shows it to the user in a message box
    tkMessageBox.showinfo("User Input", E1.get())


def quit():
    #This function closes the root window
    root.destroy()


root = Tk() #specify the root window

label1 = Label( root, text="User Input") #specify the label to show the user what the text area is about
E1 = Entry(root, bd =5) #specify the input text area

submit = Button(root, text ="Submit", command = getInfo) #specify the "submit" button that runs "getInfo" when clicked
quit_button = Button(root, text = 'Quit', command=quit) #specify the quit button that quits the main window when clicked

#Add the items we specified to the window
label1.pack()
E1.pack()
submit.pack(side =BOTTOM)
quit_button.pack(side =BOTTOM) 

root.mainloop() #Run the "loop" that shows the windows