在 Python 中弹出图形对话框的最简单的跨平台方法是什么?
问题描述
我想要在 Python 脚本中弹出简单对话框的最简单方法.理想情况下,解决方案是:
- 在 Windows、OS X、Gnome、KDE 上工作
- 看起来像任何操作系统上的原生对话框
- 需要最少的代码
要弹出一个简单的标准对话框,只需要最少的代码.本质上,您只是说弹出一个带有此文本的标准对话框",或弹出一个带有问题 x 的对话框并将响应输入变量 y".
这适用于在命令行上运行的简单脚本.我不想了解 GUI 框架,也不想设置启动 GUI 线程、注册事件处理程序、配置一些窗口属性、运行循环"等代码.我不想设置之后打开一个窗口或关闭窗口.我给它文本以放入窗口和/或按钮和/或复选框,它返回用户点击的内容.其他一切都应该自动处理.例如:
message_box('文件转换完成')
对于带有确定"按钮的标准对话框,或
balloon_tip('文件转换完成')
用于系统托盘弹出气球,或
format = button_box('你想要哪种文件格式?', 'JPG', 'PNG')
他们按下两个按钮之一,然后 format
等于 'JPG'
,或者
response = text_query('你想给文件取什么名字?')
在他们输入框并按确定后,response
现在等于 'bananas.txt'
.无需其他代码.可怜的用户没有丑陋的命令行提示.
我将 Zenity 和 EasyGUI 列为示例答案,因为它们与我想要的相似,但并不完美.
[ 和 文档非常整洁.
之前,还有一个名为 EasyGuiTtk 的分支,遗憾的是不再可用.p>
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]
解决方案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')
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.
相关文章