Alert boxes in Python?
Asked Answered
J

6

42

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.

Jago answered 7/10, 2008 at 5:8 Comment(1)
I suppose a better way to phrase this is as a "Background process" not a daemon.Jago
R
77

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.

Robinetta answered 7/10, 2008 at 5:29 Comment(4)
For more info on this function I found this: docs.activestate.com/activepython/2.4/pywin32/…Jago
I'm getting ImportError: No module named win32api.Pattipattie
install the package with easy_install PyWin32. Unfortunately there is no built-in solution for that aside from the very restricted raw_imput(), right?Finkle
Or you can use ActiveState's version of Python, activestate.com/activepython/downloads, which comes with PyWin32 already installed.Transformation
C
21

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

Cronus answered 10/9, 2019 at 12:59 Comment(4)
One thing...the message box won't close after I press ok with your code.... Why?Pauiie
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?Cronus
I have the same issue as @AIM_BLB. Clicking ok won't close the windowEmblem
@AbdulkarimMalkadi What version of python and what version of tkinter are you using?Cronus
S
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 *
Satinwood answered 16/11, 2020 at 13:25 Comment(0)
P
3

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)
Predikant answered 6/8, 2012 at 15:23 Comment(1)
you are importing pythoncom and win32com.client for nothingDiscordant
S
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.

Slipslop answered 9/12, 2013 at 1:14 Comment(2)
Somehow this gives me an error "no module named repository". Seems like it is outdated?Sequacious
@Sequacious You on Ubuntu 16.04 or newer? Try it in python3. :)Slipslop
W
-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.

Woodyard answered 7/10, 2008 at 5:30 Comment(3)
That seems like an awful lot of work, and it does not solve teh problem.Jago
You did not specify the conditions upon which the dialog box would be displayed. I assumed inter-application dependencies.Woodyard
I said I needed to produce an alert box, this does not display an alert.Jago

© 2022 - 2024 — McMap. All rights reserved.