Shimon Pokrivka
Shimon Pokrivka

Reputation: 55

Tkinter Messagebox - Save Messagebox answers (eg. Does user pick 'Yes 'or 'No')

So I'm writing an app in Python 3.6, but I can't seem to find an answer on how to get the value of a messagebox answer (Yes or No).

fileSavedExit = True
def msgbox1():
    if fileSavedExit == True:
        root.destroy()  
    if fileSavedExit == False:
        messagebox.askyesno('Save File ?','Do you want to save the file first?')

Something like that. I'm looking for code that would save the answer ('Yes' or 'No'). Would be thankful if anyone would help me. :O

Upvotes: 0

Views: 3056

Answers (1)

Nae
Nae

Reputation: 15335

askyesno returns True if the answer was "Yes" and False if the answer was "No". You can simply filter it with if/else.

Example

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
    import tkinter.messagebox as tkmb
except ImportError:
    import Tkinter as tk
    import tkMessageBox as tkmb


def ask():
    response = tkmb.askyesno("Messagebox Title", "Is this it?")
    global button
    if response:    # If the answer was "Yes" response is True
        button['text'] = "Yes"
    else:           # If the answer was "No" response is False
        button['text'] = "No"


if __name__ == '__main__':
    root = tk.Tk()
    button = tk.Button(root, text="Ask...", command=ask)
    button.pack()
    tk.mainloop()

Upvotes: 2

Related Questions