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 17:08

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

查看更多
对你真心纯属浪费
3楼-- · 2019-01-21 17:10

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/

查看更多
戒情不戒烟
4楼-- · 2019-01-21 17:11

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.

查看更多
男人必须洒脱
5楼-- · 2019-01-21 17:13

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

查看更多
登录 后发表回答