Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a message box with tkinter?

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?

like image 557
user46646 Avatar asked Jun 27 '09 08:06

user46646


People also ask

How do I show pop up messages in Tkinter?

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.

What is Tkinter message?

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.


2 Answers

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.

like image 94
Tyler Avatar answered Oct 07 '22 14:10

Tyler


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()
like image 37
user1497423 Avatar answered Oct 07 '22 13:10

user1497423