What is a simple cross-platform way to display graphical dialogs in Python?

I need the easiest way to display simple dialogs in a Python script. Ideally, the solution would be:

  • Work with Windows, OS X, Gnome, KDE
  • See how the native dialogue on any OS
  • Require minimum code

Only a minimal code is required to display a simple standard dialog. In fact, you simply say "pop up a standard dialog box with this text" or "pop up a dialog box with a question x and the response of the feed to the variable y".

This is for simple scripts that would otherwise be executed on the command line. I don’t want to know about the graphical user interface infrastructure or I need to install code that says: “Run the GUI thread, register an event handler, configure some window properties, start the loop”, etc. I do not want to install then close the window or close the window. I give him the text to enter in the window and / or buttons and / or flags, it returns what the user clicked. Everything else should be taken care of automatically. For example:

message_box('File conversion complete') 

for a standard dialog box with an OK button or

 balloon_tip('File conversion complete') 

for the system tray popup window, or

 format = button_box('Which file format do you want?', 'JPG', 'PNG') 

and press one of the two buttons, and then format is 'JPG' or

 response = text_query('What would you like to name the file?') 

and after they type in the field and click OK, response now equal to 'bananas.txt' . No other code is required. The lack of an ugly command line tells the poor user.

I cited Zenity and EasyGUI as examples of answers, as they are similar to what I want, but not perfect.

[ Previously asked a question on the Python forum ]

+36
python cross-platform dialog zenity
Oct 28 '09 at 3:20
source share
10 answers

EasyGUI is a single file and provides an easy way to work with Tkinter dialogs, but they are still ugly, native Tkinter dialogs.

 from easygui import msgbox msgbox('Stuff') 

Tkinter is ugly on UbuntuTKinter is ugly on Windows 7

It can be easily installed using:

 $ sudo pip3 install --upgrade easygui 

There is a GitHub repository and the documentation is very neat.

Previously, there was also an EasyGuiTtk fork, which, unfortunately, is no longer available.

enter image description here

+21
Oct 28 '09 at 3:25
source share

Zenity runs on Linux and Windows and can be called directly from Python:

 import os os.system('zenity --info --text="Stuff"') 

The return values ​​from the question blocks must be obtained for the action, although this is a more complex process, and you need to learn how to interact with subprocesses, etc.

It can also be used with the PyZenity interface , which simplifies the collection of return values:

 from PyZenity import InfoMessage InfoMessage('Stuff') 

I tested PyZenity in both Ubuntu and Windows XP, and it works in both.

Zenity looks pretty good in gnomeZenity looks good in KDE, too, suprisinglyZenity in Windows has the wrong GTK theme

I read that Zenity is only GTK +, but I tried it in Gnome and KDE, and it looks like in both. However, the port for Windows does not look native because it uses the wrong GTK theme?

Are there other programs, such as KDialog and Xdialog , that can be paired with a similar Python frontend that could check and see which programs are available so that it automatically takes care of everything? (There is also a Ruby frontend for KDialog .)

I do not know if PyZenity works under OS X.

+13
Oct 28 '09 at 3:26
source share

TkInter usually ships with Python

 # File: hello1.py from Tkinter import * root = Tk() w = Label(root, text="Hello, world!") w.pack() root.mainloop() 

If you need something more familiar, you will need to install something like wxpython

+11
Oct 28 '09 at 3:24
source share

To extend on endolith tkMessageBox the answer with an ugly empty window in the background ...

In the code below, a window without a background window appears.

 import Tkinter, tkMessageBox root = Tkinter.Tk() root.withdraw() tkMessageBox.showinfo("my dialog title", "my dialog message") 

This is taken directly from the helpful comment I found at the bottom of this article . Thanks to Jason (commentator) and effbot.org.

+8
Feb 20 2018-11-21T00:
source share

The PyMsgBox module does almost that. It uses the tkinter built-in module for its message box functions that follow the JavaScript naming conventions: alert (), confirm (), prompt (), and password () (which is a prompt () but uses * as input). These function calls are blocked until the user presses the OK / Cancel button. This is a cross-platform, clean Python module with no dependencies.

Future versions will have their own explanatory message boxes.

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/

+8
Sep 04 '14 at 0:24
source share

@endolith, re: zenity for Windows.

Hello,

I repackaged "Zenity for Windows" and included the correct GTK theme file. Now it looks a lot better. :) Now it is available for download: http://www.placella.com/software/zenity/

Screenshot:

alt text
(source: placella.com )

World, Ruslan

+6
Nov 02 '09 at 21:49
source share

wxPython is the best Python GUI library (IMO) and uses its own widgets.

 import wx app = wx.PySimpleApp() dialog = wx.MessageDialog(None, 'wxPython is awesome!', 'Dialog Box', wx.OK|wx.ICON_INFORMATION) dialog.ShowModal() dialog.Destroy() app.MainLoop() 
+5
Nov 05 '09 at 14:36
source share

Another possibility is the tkMessageBox module , which is apparently built into the standard library and is cross-platform, although it is even more ugly than the rest:

 import tkMessageBox tkMessageBox.showinfo('Title','Stuff') 

Tkinter is super ugly

+3
Oct 31 '09 at 0:38
source share

pyglet is another alternative, although it may not be the easiest. that, being said, it is cross-platform and depends only on python, so there are no external dependencies. this fact in itself may be sufficient reason to use it over others.

and all this can easily handle multimedia, quite convenient if you want to display an image or video or something like that.

the example below is from the documentation ...

 #!/usr/bin/python import pyglet window = pyglet.window.Window() label = pyglet.text.Label('Hello, world', font_name='Times New Roman', font_size=36, x=window.width/2, y=window.height/2, anchor_x='center', anchor_y='center') @window.event def on_draw(): window.clear() label.draw() pyglet.app.run() 
+1
Oct 28 '09 at 16:48
source share

It's impossible. If you want to simply, then you should use Tkinter, because that is what is included. If Tkinter is not good enough, you will have to choose and package a graphical interface for each platform separately.

I suggest you use Tkinter and wrap the parts you need in a class that will be even easier to use.

-3
Nov 05 '09 at 14:47
source share



All Articles