在Python中弹出图形对话框的最简单的跨平台方式是什么? [英] What's the simplest cross-platform way to pop up graphical dialogs in Python?
问题描述
我想用最简单的方式在Python脚本中弹出简单的对话框。理想情况下,解决方案将是:
- 在Windows,OS X,Gnome,KDE上工作
- 查看像任何操作系统上的本地对话框
- 需要最小代码
弹出一个简单的标准对话框只需要最少的代码。基本上你只是说弹出一个这个文本的标准对话框,或者弹出一个带有问题x的对话框,并将响应添加到变量y。
是用于在命令行上运行的简单脚本。我不想知道GUI框架,或者必须设置代码,说启动GUI线程,注册一个事件处理程序,配置一些窗口属性,运行循环等。我不想要设置一个窗户或之后关闭窗户。我给它放入窗口和/或按钮和/或复选框的文本,它返回用户点击的内容。一切都应该自动照顾。例如:
message_box('文件转换完成')
对于带有确定按钮的标准对话框,或
$ b
format = button_box('你想要哪种文件格式','JPG','PNG')
,他们按两个按钮之一,然后格式
等于'JPG'
或
response = text_query('你想要命名什么文件?')
,他们输入框后按确定,回应
现在等于'bananas.txt'
。不需要其他代码没有丑陋的命令行提示糟糕的用户。
我列出了Zenity和EasyGUI作为示例答案,因为它们与我想要的类似,但不完美。
[,可以通过 pip install https://github.com/Who8MyLunch/EasyGuiTtk/archive/master.zip
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')
Apparently it is now abandonware, but there's also a fork called EasyGuiTtk, obtainable via pip install https://github.com/Who8MyLunch/EasyGuiTtk/archive/master.zip
这篇关于在Python中弹出图形对话框的最简单的跨平台方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!