0

trying to open and close a popup window without waiting for the user's response.

I have tried:

    root = Tk()
    root.withdraw()
    try:
        root.after(TIME_TO_WAIT, root.destroy)
        messagebox.showinfo("Output", "somthing")
    except:
        pass

or:

    root = Tk()
    root.withdraw()
    try:
        root.after(TIME_TO_WAIT, root.destroy)
        Message(title="your title", message="somthing", master=root).show()
    except:
        pass

but it throws me the following using macOS:

2023-02-28 15:12:08.197 Python[10165:332611] Warning: Expected min height of view: (<NSButton: 0x7fe48210d240>) to be less than or equal to 30 but got a height of 32.000000. This error will be logged once per view in violation.
2023-02-28 15:12:50.709 Python[10165:332611] Warning: Expected min height of view: (<NSButton: 0x7fe480190bf0>) to be less than or equal to 30 but got a height of 32.000000. This error will be logged once per view in violation.
objc[10165]: autorelease pool page 0x7fe47c277000 corrupted
magic     0x00000000 0x00000000 0x00000000 0x00000000
should be 0xa1a1a1a1 0x4f545541 0x454c4552 0x21455341
pthread   0x7ff85525f4c0
should be 0x7ff85525f4c0
Leah Golub
  • 15
  • 4

2 Answers2

0

I would do something along the lines of:

import tkinter as tk
import tkinter.messagebox as msgbox

def showMessage(message, timeout=5000):
    root = tk.Tk()
    root.withdraw()
    root.after(timeout, root.destroy)
    msgbox.showinfo('Info', message, master=root)

showMessage('Your Message Here')
OPT
  • 1
  • 2
  • This works, but exits with an exception: `_tkinter.TclError: can't invoke "grab" command: application has been destroyed`. You can suppress this by wrapping the contents of `showMessage` in a `try` block followed by `except tk.TclError: pass`. – JRiggles Feb 28 '23 at 13:56
  • it still doesn't work, it throws me the same thing. It is because of the line of root.after(timeout, root.destroy), it does it on the root.destroy part. Online I saw it is something to do with macOS but haven't got a clear understanding. – Leah Golub Feb 28 '23 at 14:00
  • @LeahGolub, are you using the system Python / tkinter by any chance? That error message doesn't look like a typical Python traceback to me. – JRiggles Feb 28 '23 at 14:02
  • @LeahGolub try adding: root.minsize(30, 30) after root = tk.Tk() – OPT Feb 28 '23 at 14:03
  • @OPT it still doesn't work. I understand what you were trying to do but. – Leah Golub Feb 28 '23 at 14:09
  • @JRiggles I think it is more maybe a OS issue and not a python issue. But what do you mean "system Python / tkinter"? – Leah Golub Feb 28 '23 at 14:10
  • @LeahGolub this may be the same thing, if it's an OS issue: https://stackoverflow.com/a/70883023/21304200 – OPT Feb 28 '23 at 14:24
  • @LeahGolub Mac OS comes with an older Python (and by extension, tkinter) built-in since the system needs it. If you want to do your own Python development on Mac OS, you should install Python on your own with something like [Homebrew](https://brew.sh) or from [python.org](https://python.org). [This answer](https://stackoverflow.com/a/73186351/8512262) of mine might help, even though the question was about a different issue – JRiggles Feb 28 '23 at 14:24
  • @OPT I've tried what you sent earlier today, but somehow it seamed to miss up my system – Leah Golub Feb 28 '23 at 14:46
  • @JRiggles I understand now what you meant, I did install python and it is not a built in python, and just now I've tried to " brew install python-tk didn't solve the problem – Leah Golub Feb 28 '23 at 14:52
  • @LeahGolub Have you tried `brew install tcl-tk`? – JRiggles Feb 28 '23 at 14:55
  • @JRiggles I've tried now, but still didn't work. Maybe there is a different way to have a popup window that I could use with out waiting for the user's response? – Leah Golub Feb 28 '23 at 15:10
  • @LeahGolub I've submitted an answer that might work, but if your Python/tkinter installation is damaged, it may not help – JRiggles Feb 28 '23 at 15:35
  • I have used "process" and it works now:) – Leah Golub Mar 02 '23 at 10:01
0

Here's a potential solution that creates a Toplevel window that closes automatically after 5 seconds.

import tkinter as tk


def close():
    dialog.destroy()
    root.quit()


root = tk.Tk()
root.withdraw()
root.after(5000, close)
# create dialog window
dialog = tk.Toplevel(root)
dialog.geometry('200x100')
dialog.title('Hello')
message = tk.Label(dialog, text='Please Wait...')
message.pack(expand=True, fill='both')


# run app
if __name__ == '__main__':
    root.mainloop()

But if you're not going to bother with tkinter.messagebox, you can skip creating the Toplevel window and simply use the main window as your message dialog! :)

import tkinter as tk


root = tk.Tk()
root.geometry('200x100')
root.title('Hello')
root.after(5000, root.quit)  # quit the app automatically after 5 sec
message = tk.Label(root, text='Please Wait...')
message.pack(expand=True, fill='both')


# run app
if __name__ == '__main__':
    root.mainloop()
JRiggles
  • 4,847
  • 1
  • 12
  • 27