Alert boxes in Python?

Question:

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.

Asked By: UnkwnTech

||

Answers:

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.

Answered By: weir

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:yourfile. After, say, 1 second of no activity, you display the message and truncate the file.

Answered By: Mikael Jansson

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)
Answered By: Tabares

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.

Answered By: NoBugs

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

Answered By: Matt Binford

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 *
Answered By: mathcat
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.