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

后端 未结 10 2152
一生所求
一生所求 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:45

    @ 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:


    (source: placella.com)

    Peace, Rouslan

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

    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

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

    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/

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

    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()
    
    0 讨论(0)
提交回复
热议问题