How to create a message box using tkinter?

I am trying to create a fairly simple message box in tkinter with the "YES" and "NO" buttons. When I press the "YES" button inside, it should go and write YES to the file. Similarly, when "NO" is pressed, NO must be written to the file. How can i do this?

+8
python tkinter messagebox
Jun 27 '09 at 8:48
source share
3 answers

You can use the tkMessageBox module for Python 2.7 or the corresponding version for Python 3 called tkinter.messagebox .

askquestion() seems to be exactly what you want. It will even return the string "yes" or "no" for you.

+18
Jun 27 '09 at 8:54
source share

You can assign the return value of the askquestion function askquestion variable, and then simply write the variable to a file:

 from tkinter import messagebox variable = messagebox.askquestion('title','question') with open('myfile.extension', 'w') as file: # option 'a' to append file.write(variable + '\n') 
+8
Dec 10 '12 at 9:10
source share

Here you can ask a question using the message box in Python 2.7. You need the tkMessageBox module 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() 
+7
Jul 16 2018-12-12T00:
source share



All Articles