views:

570

answers:

8

I want the simplest possible way to pop up simple dialogs in Python scripts. Ideally, the solution would:

  • Work on Windows, OS X, Gnome, KDE
  • Look like a native dialog on any OS
  • Require minimal code

To pop up a simple standard dialog should require only minimal code. Essentially you're just saying "Pop up a standard dialog with this text", or "Pop up a dialog with question x and feed response into variable y".

I don't want to know about GUI frameworks or have to set up code that says "start a GUI thread, register an event handler, configure some window properties, run a loop", etc. I don't want to have to set up a window or close the window afterward. I give it the text to put in the window, it tells me what the user clicked on. Everything else should be taken care of automatically.

I've listed Zenity and EasyGUI as example answers, since they're similar to what I want, but not perfect.

Previously asked here

+3  A: 

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

gnibbler
There's also tkMessageBox.showinfo('Title','Stuff')
endolith
+3  A: 
endolith
Tk need not be ugly, not since Python 2.7 anyway ... which includes ttk: http://code.google.com/p/python-ttk/wiki/Screenshots
Sridhar Ratnakumar
But how difficult is it to make it non-ugly in a cross-platform way?
endolith
+1  A: 
endolith
A: 

pyglet is another alternative, though it may not be the simplest. that being said, it's cross-platform and only depends on python, so there's no external dependencies. that fact alone can be reason enough to use it over others.

and all it can handle multimedia pretty easily as well, pretty handy if you want to display an image or video or something.

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()
Rayjan
+1  A: 
endolith
+1  A: 

@ endolith, re: zenity for Windows.

Hi,

I repackaged "Zenity for Windows" and included the correct GTK-theme file. It looks much better now. :) It is now available for download: http://www.placella.com/software/zenity/

Screenshot:

alt text

Peace, Rouslan

Roccivic
If it's not difficult, then it should be that way by default! :) Does that theme work on any version of Windows or just XP?
endolith
Hi, I corrected this issue and edited the above post to reflect such changes.Oh, and I'm not sure about Vista or W7, maybe someone else will post feedback...Enjoy, everyone!Peace, Rouslan
Roccivic
+1  A: 

wxPython is the best Python GUI library (IMO) and uses native 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()
FogleBird
That's not too terrible. I don't understand why it's not just a one-liner, though.import wxwx.OpenADialogBox('Some text')
endolith
Because most people using a GUI want more than just a single popup. You are an edge case.
jmucchiello
I think there are a *lot* of people who would use dialogs like this in their scripts if they were available.
endolith
A: 

This is not possible. If you want simple then you have to use Tkinter because that is what is included. If Tkinter is not good enough then you will have to choose and package a GUI for each platform separately.

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

Michael Dillon