3

I know you can use something like, self.root.after(1000, self.update_clock)

But could I some how replace that second function with a function that's similar to messagebox.showinfo.destroy()? I'm basically trying to put these message boxes on a timer so that the user will see them but won't have to do anything themselves.

response = tkinter.messagebox.showinfo("Warning!", "New artist object has been     created: "
                                                       + "\n" + "$oid: " + str(self.artistObjectId))

if response == "ok":
            self.currentState += 1
            self.states[self.currentState](importedTracks[self.currentTrack])
terratunaz
  • 614
  • 3
  • 9
  • 19

2 Answers2

4

Maybe a message box is not what you require in this context. If you would just like to show a message then have it automatically disappear you could use a new TopLevel or frame and then destroy the frame after a timeout. In terms of user interaction and experience, message boxes are designed to wait for user input?

This is a good example of using a new TopLevel

closing tkmessagebox after some time in python

I found this page that describes what can be done to customise message boxes, though what I could find is somewhat limited.

http://effbot.org/tkinterbook/tkinter-standard-dialogs.htm

BenJ
  • 456
  • 2
  • 7
  • Thanks, that's a good idea. Great examples too. I just wanted to make sure there wasn't already a way to do it with the message boxes. – terratunaz Jan 06 '16 at 23:06
0

The small function below will do the job. By setting the type you can choose for: info, warning or error message box, the default is 'Info'. You can set also the timeout, the default is 2.5 seconds.

def showMessage(message, type='info', timeout=2500):
    import tkinter as tk
    from tkinter import messagebox as msgb

    root = tk.Tk()
    root.withdraw()
    try:
        root.after(timeout, root.destroy)
        if type == 'info':
            msgb.showinfo('Info', message, master=root)
        elif type == 'warning':
            msgb.showwarning('Warning', message, master=root)
        elif type == 'error':
            msgb.showerror('Error', message, master=root)
    except:
        pass

Call the function as follow: For message type 'Info' and timeout of 2.5 seconds:

showMessage('Your message')

Or by your own settings for type message 'Error' and timeout 4 seconds:

showMessage('Your message', type='error', timeout=4000)