如何创建与Tkinter的一个消息框?(How to create a message box wi

2019-06-24 07:22发布

我一直在试图建立一个相当简单的消息框在具有“是”和“否”按钮Tkinter的。 当我按下“是”按钮,在内部它必须去YES写入文件。 同样,当“NO”被按下,NO必须写入到文件中。 我怎样才能做到这一点?

Answer 1:

您可以使用该模块tkMessageBox为Python 2.7或为Python 3名为对应的版本tkinter.messagebox

它看起来像askquestion()正是你想要的功能。 它甚至将返回字符串"yes""no"为您服务。



Answer 2:

下面是你可以在Python 2.7使用一个消息框问一个问题。 特别需要的模块tkMessageBox

from Tkinter import *
import tkMessageBox


root = Tk().withdraw()  # hiding the main window
var = tkMessageBox.askyesno("Title", "Your question goes here?")

filename = "log.txt"

f = open(filename, "w")
f.write(str(var))
print str(var) + " has been written to the file " + filename
f.close()


Answer 3:

您可以将返回值分配askquestion功能给一个变量,然后你只需写入变量到一个文件:

from tkinter import messagebox

variable = messagebox.askquestion('title','question')

with open('myfile.extension', 'w') as file: # option 'a' to append
    file.write(variable + '\n')


文章来源: How to create a message box with tkinter?