What's the simplest cross-platform way to pop up graphical dialogs in Python?

后端 未结 10 2151
一生所求
一生所求 2020-12-13 04:15

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
相关标签:
10条回答
  • 2020-12-13 04:28

    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.

    0 讨论(0)
  • 2020-12-13 04:31

    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')
    

    Tkinter is ugly on Ubuntu TKinter is ugly on Windows 7

    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.

    enter image description here

    0 讨论(0)
  • 2020-12-13 04:34

    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()
    
    0 讨论(0)
  • 2020-12-13 04:34

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

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

    Tkinter is super ugly

    0 讨论(0)
  • 2020-12-13 04:42

    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.

    0 讨论(0)
  • 2020-12-13 04:43

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

    import os
    os.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.

    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.

    0 讨论(0)
提交回复
热议问题