問題描述
我想要在 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.
這篇關于在 Python 中彈出圖形對話框的最簡單的跨平臺方法是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!