12

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?

Air
  • 8,274
  • 2
  • 53
  • 88
user46646
  • 153,461
  • 44
  • 78
  • 84

5 Answers5

21

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.

nbro
  • 15,395
  • 32
  • 113
  • 196
Tyler
  • 21,762
  • 11
  • 61
  • 90
12

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()
nbro
  • 15,395
  • 32
  • 113
  • 196
user1497423
  • 707
  • 7
  • 3
  • Link to the othe types of message boxes http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/tkMessageBox.html – Nande May 28 '18 at 22:39
8

You can assign the return value of the askquestion function to a variable, and then you 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')
nbro
  • 15,395
  • 32
  • 113
  • 196
Alyson
  • 81
  • 1
  • 1
1

You can use message box from Tkinter using

# For python 3 or above
from tkinter import messagebox

# For python less than 3
from Tkinter import *
import tkMessageBox

Here's the basic syntax:

messagebox.Function_Name(title, message [, options])

The message boxes are modal and will return a subset of (True, False, OK, None, Yes, No) based on the user’s selection.

So to get the value of message box you just need to store the value in a variable. An example is given below:

res=mb.askquestion('Exit Application', 'Do you really want to exit')
if res == 'yes' :
    root.destroy()

There are different type of message boxes or Function_name:

  1. showinfo(): Show some relevant information to the user.
  2. showwarning(): Display the warning to the user.
  3. showerror(): Display the error message to the user.
  4. askquestion(): Ask question and user has to answered in yes or no.
  5. askokcancel(): Confirm the user’s action regarding some application activity.
  6. askyesno(): User can answer in yes or no for some action.
  7. askretrycancel(): Ask the user about doing a particular task again or not.
  8. Message: creates a default information box, or is same as showinfo box.
messagebox.Message(master=None, **options)
# Create a default information message box.

You can even pass your own title, message and even modify the button text in it.

Things you can pass in the message boz:

  • title: This parameter is a string which is shown as a title of a message box.
  • message: This parameter is the string to be displayed as a message on the message box.
  • options: There are two options that can be used are:
    1. default: This option is used to specify the default button like ABORT, RETRY, or IGNORE in the message box.
    2. parent: This option is used to specify the window on top of which the message box is to be displayed.

Links i have used for this answer:

Faraaz Kurawle
  • 1,085
  • 6
  • 24
-1

You don't need any other modules to do this!

>>> from tkinter import messagebox
>>> messagebox.askokcancel("Title", "Message")
True

It returns True when the ok/retry/yes button is pressed, and False otherwise.

Other available ones:

  • askokcancel()
  • askyesno() or askquestion() (same)
  • askretrycancel()
Gugalcrom123
  • 101
  • 1
  • 9