How can I use an alert dialog with Python in linux?
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()
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")
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.
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.