【问题标题】:Show user a message in python在 python 中向用户显示消息
【发布时间】:2015-10-15 20:20:38
【问题描述】:

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

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

【问题讨论】:

  • 您是指在控制台中,还是在 GUI 对话框中?
  • 哪个操作系统,哪个 GUI 工具包?视窗?苹果? Linux?无论哪种方式,这都是重复的,请阅读与您的操作系统和 GUI 工具包相关的问题。
  • “最好”在 GUI 中?所以你愿意接受控制台解决方案吗?例如,您不熟悉print 语句?也许你应该看看official Python tutorial

标签: python text output


【解决方案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

【讨论】:

  • @Ryrysmithers,这是你要的吗?如果是,请将答案标记为已接受;如果不是,请澄清。
猜你喜欢
  • 1970-01-01
  • 2019-02-16
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
  • 1970-01-01
相关资源
最近更新 更多