41

Is it possible to produce an alert similar to JavaScript's alert("message") in python, with an application running as a daemon.

This will be run in Windows, Most likely XP but 2000 and Vista are also very real possibilities.

Update:
This is intended to run in the background and alert the user when certain conditions are met, I figure that the easiest way to alert the user would be to produce a pop-up, as it needs to be handled immediately, and other options such as just logging, or sending an email are not efficient enough.

UnkwnTech
  • 88,102
  • 65
  • 184
  • 229

6 Answers6

73

what about this:

import win32api

win32api.MessageBox(0, 'hello', 'title')

Additionally:

win32api.MessageBox(0, 'hello', 'title', 0x00001000) 

will make the box appear on top of other windows, for urgent messages. See MessageBox function for other options.

user
  • 5,335
  • 7
  • 47
  • 63
  • 2
    For more info on this function I found this: http://docs.activestate.com/activepython/2.4/pywin32/win32api__MessageBox_meth.html – UnkwnTech Oct 07 '08 at 05:40
  • 11
    I'm getting `ImportError: No module named win32api`. – user May 28 '12 at 11:00
  • 1
    install the package with **`easy_install PyWin32`**. Unfortunately there is no built-in solution for that aside from the very restricted `raw_imput()`, right? – ewerybody Apr 09 '15 at 19:30
  • Or you can use ActiveState's version of Python, http://www.activestate.com/activepython/downloads, which comes with PyWin32 already installed. – twasbrillig Jun 30 '15 at 18:23
20

For those of us looking for a purely Python option that doesn't interface with Windows and is platform independent, I went for the option listed on the following website:

https://pythonspot.com/tk-message-box/ (archived link: https://archive.ph/JNuvx)

# Python 3.x code
# Imports
import tkinter
from tkinter import messagebox

# This code is to hide the main tkinter window
root = tkinter.Tk()
root.withdraw()

# Message Box
messagebox.showinfo("Title", "Message")

You can choose to show various types of messagebox options for different scenarios:

  • showinfo()
  • showwarning()
  • showerror ()
  • askquestion()
  • askokcancel()
  • askyesno ()
  • askretrycancel ()

edited code per my comment below

Matt Binford
  • 620
  • 8
  • 15
  • 1
    One thing...the message box won't close after I press ok with your code.... Why? – ABIM Mar 12 '20 at 16:05
  • I noticed that I didn't put a () after withdraw. Try that. Other than that, I don't have any problems with the meesagebox closing. Are you using it exactly as written? What version of python are you using? – Matt Binford Mar 13 '20 at 19:44
  • I have the same issue as @AIM_BLB. Clicking ok won't close the window – AKMalkadi Aug 14 '21 at 17:55
  • @AbdulkarimMalkadi What version of python and what version of tkinter are you using? – Matt Binford Aug 16 '21 at 12:00
6

You can use PyAutoGui to make alert boxes First install pyautogui with pip:

pip install pyautogui

Then type this in python:

import pyautogui as pag
pag.alert(text="Hello World", title="The Hello World Box")

Here are more message boxes, stolen from Javascript:

  • confirm()
    With Ok and Cancel Button
  • prompt()
    With Text Input
  • password() With Text Input, but typed characters will be appeared as *
math scat
  • 310
  • 8
  • 17
3

GTK may be a better option, as it is cross-platform. It'll work great on Ubuntu, and should work just fine on Windows when GTK and Python bindings are installed.

from gi.repository import Gtk

dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO,
            Gtk.ButtonsType.OK, "This is an INFO MessageDialog")
dialog.format_secondary_text(
    "And this is the secondary text that explains things.")
dialog.run()
print "INFO dialog closed"

You can see other examples here. (pdf)

The arguments passed should be the gtk.window parent (or None), DestroyWithParent, Message type, Message-buttons, title.

NoBugs
  • 9,310
  • 13
  • 80
  • 146
2

You can use win32 library in Python, this is classical example of OK or Cancel.

import win32api
import win32com.client
import pythoncom

result = win32api.MessageBox(None,"Do you want to open a file?", "title",1)

if result == 1:
 print 'Ok'
elif result == 2:
 print 'cancel'

The collection:

win32api.MessageBox(0,"msgbox", "title")
win32api.MessageBox(0,"ok cancel?", "title",1)
win32api.MessageBox(0,"abort retry ignore?", "title",2)
win32api.MessageBox(0,"yes no cancel?", "title",3)
Tabares
  • 4,083
  • 5
  • 40
  • 47
-4

Start an app as a background process that either has a TCP port bound to localhost, or communicates through a file -- your daemon has the file open, and then you echo "foo" > c:\your\file. After, say, 1 second of no activity, you display the message and truncate the file.

Mikael Jansson
  • 4,801
  • 1
  • 24
  • 14