What's The Simplest Cross-platform Way To Pop Up Graphical Dialogs In Python?
Solution 1:
EasyGUI is a single file, and provides a simple way to work with Tkinter dialogs, but they're still ugly non-native Tkinter dialogs.
from easygui import msgbox
msgbox('Stuff')
It can easily be installed using:
$ sudo pip3 install --upgrade easygui
There is a GitHub repository and documentation is very neat.
Previously, there was also a fork called EasyGuiTtk, which unfortunately is no longer available.
Solution 2:
Zenity works under Linux and Windows, and can be called from Python directly:
import osos.system('zenity --info --text="Stuff"')
Using --warning
instead of --info
gives a warning dialog box instead of an info box. Other options can be found here: https://help.gnome.org/users/zenity/stable/
The return values from question boxes need to be captured for acting on, though, which is more complex, and you have to learn about communicating with subprocesses, etc.
It can also be used with the PyZenity front-end, which makes capturing return values simple:
from PyZenity import InfoMessage
InfoMessage('Stuff')
I have tested PyZenity in both Ubuntu and Windows XP, and it works in both.
I read that Zenity is GTK+ only, but I tried it in Gnome and KDE and it looks native in both. The port to Windows does not look native, though, because it uses the wrong GTK theme?
There are also other programs like KDialog and Xdialog that might be interfaced to a similar Python frontend that could check and see what executables are available so that it automatically takes care of everything? (There's a Ruby frontend for KDialog, too.)
I don't know if PyZenity works under OS X, either.
Solution 3:
TkInter is usually supplied with Python
# File: hello1.py
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
If you want something more native looking, you'll have to install something like wxpython
Solution 4:
The PyMsgBox module does almost exactly this. It uses the built-in tkinter module for its message box functions that follow the naming conventions of JavaScript: alert(), confirm(), prompt() and password() (which is prompt() but uses * when you type). These function calls block until the user clicks an OK/Cancel button. It's a cross-platform, pure Python module with no dependencies.
Native look-and-feel message boxes will be available in a future version.
Install with: pip install PyMsgBox
Sample usage:
>>>import pymsgbox>>>pymsgbox.alert('This is an alert!', 'Title')>>>response = pymsgbox.prompt('What is your name?')
Full documentation at http://pymsgbox.readthedocs.org/en/latest/
Solution 5:
To extend on endolith's tkMessageBox answer with the ugly empty window in the background...
The code below pops up the box without the background window.
import Tkinter, tkMessageBoxroot= Tkinter.Tk()
root.withdraw()
tkMessageBox.showinfo("my dialog title", "my dialog message")
This is lifted directly from a useful comment I found at the bottom of this article. Thanks to Jason (the commenter) and effbot.org.
Post a Comment for "What's The Simplest Cross-platform Way To Pop Up Graphical Dialogs In Python?"