What's the simplest cross-platform way to pop

2019-01-21 16:43发布

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".

This is for simple scripts that would otherwise run on the command line. 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 and/or buttons and/or checkboxes, it returns what the user clicked on. 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 a system tray popup balloon, or

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

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

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

and after they type in the box and press Ok, response now equals 'bananas.txt'. No other code required. No ugly command line prompts for the poor user.

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

[Previously asked on Python Forum]

10条回答
乱世女痞
2楼-- · 2019-01-21 16:49

Zenity works under Linux and Windows, and can be called from Python directly:

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

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.

Zenity looks pretty good in Gnome Zenity looks good in KDE, too, suprisingly Zenity in Windows has the wrong GTK theme

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.

查看更多
混吃等死
3楼-- · 2019-01-21 16:52

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

查看更多
beautiful°
4楼-- · 2019-01-21 16:56

@ 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 http://www.placella.com/software/zenity/zenity-win32.png

Peace, Rouslan

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-21 16:56

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()
查看更多
Juvenile、少年°
6楼-- · 2019-01-21 17:03

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, tkMessageBox
root = 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.

查看更多
Melony?
7楼-- · 2019-01-21 17:03

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()
查看更多
登录 后发表回答