I've been trying to build a fairly simple message box in tkinter that has "YES" and "NO" buttons. When I push the "YES" button internally it must go and write YES to a file. Similarly, when "NO" is pushed, NO must be written to a file. How can I do this?
Popup window in Tkinter can be created by defining the Toplevel(win) window. A Toplevel window manages to create a child window along with the parent window. It always opens above all the other windows defined in any application.
The Message widget is used to show the message to the user regarding the behaviour of the python application. The message widget shows the text messages to the user which can not be edited. The message text contains more than one line. However, the message can only be shown in the single font.
You can use the module tkMessageBox for Python 2.7 or the corresponding version for Python 3 called tkinter.messagebox
.
It looks like askquestion()
is exactly the function that you want. It will even return the string "yes"
or "no"
for you.
Here's how you can ask a question using a message box in Python 2.7. You need specifically the 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With