Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert boxes in Python?

Tags:

python

alerts

People also ask

How do you use alerts in python?

Again go to the pycharm and click terminal then paste the magical word pip install PyAutoGUI. It takes few seconds to install packages. Once the package gets installed and then go to your project alert message.

What is dialog box in Python?

Dialog boxes are a commonly used GUI element to provide feedback to the user and also to prompt the user for information or to take an action. Some examples of common dialogs are: A simple message: "Press OK to continue" Ask for "OK or cancel" Ask for "Yes, no or cancel"


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.


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


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.


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 *

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)