10

My question is similar to this question, but I'm using Xubuntu, so the win32 api is obviously not available. Is there some alternative I can use?

I just need to have a simple window pop up with a message, from a python script.

Community
  • 1
  • 1
Michael Hoffmann
  • 2,411
  • 2
  • 24
  • 42
  • 2
    There's an answer on that question that mentions gtk: would that work for you? http://stackoverflow.com/a/20461473/5031339 – NightShadeQueen Aug 30 '15 at 16:03
  • That would work, I think, but it looks like it has a lot of prerequisites. Tkinter's the way to go (at least on Ubuntu). Thanks! – Michael Hoffmann Aug 30 '15 at 16:19
  • 1
    GTK is very good, and powerful, but Tkinter's handy for simple stuff. And it's a lot easier to learn than GTK. :) – PM 2Ring Aug 30 '15 at 16:24

4 Answers4

17

You can do this with Tkinter, which is cross-platform, and commonly bundled with the standard Python package.

import Tkinter as tk
import tkMessageBox
root = tk.Tk()
root.withdraw()
tkMessageBox.showwarning('alert title', 'Bad things happened!')

(On Python 3, you need to change the first line to import tkinter as tk. And the import tkMessageBox line becomes from tkinter import messagebox, and a matching change is required for the last line).

The next two lines create a root window for the application (which all Tkinter programs need), but then make that window invisible. And finally we display our alert.

You may need to install python-tk (i.e. sudo apt-get install python-tk in Ubuntu distributions) before using Tkinter - it's not installed by default on some distributions.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Wow. That was easy. Thanks! – Michael Hoffmann Aug 30 '15 at 16:20
  • 2
    On some Linux builds, the included Python installation does not include Tkinter, because [Tcl itself isn't pre-installed](http://tkinter.unpythonic.net/wiki/How_to_install_Tkinter). This solution won't work on those systems. Just something to look out for. – TigerhawkT3 Aug 30 '15 at 16:50
8

To create a notification rather than a dialog box that needs to be dismissed, you can use notify-send as shown below. This also does not require installing python-tk or other packages.

import subprocess
subprocess.run(["/usr/bin/notify-send", "--icon=error", "This is your error message ..."])

See the man page for more options.

bitinerant
  • 1,168
  • 7
  • 24
2

This answer based on PM 2Ring answer:

I have some issue with closing the message box so I did it this way:

import Tkinter as tk
import tkMessageBox
root = tk.Tk()
root.withdraw()
tkMessageBox.showwarning('Title','Are you sure?')
root.update()
ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
1

For Python3:

from tkinter import *
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.withdraw()
messagebox.showwarning('alert title', 'Bad things happened!')

You need to install tkinter:

sudo apt-get install python3-tk 

Another solution is using pyautogui

import pyautogui as pag
pag.alert(text="Bad things happened!", title="alert title")
AKMalkadi
  • 782
  • 1
  • 5
  • 18