简体   繁体   English

在 Python 中弹出图形对话框的最简单的跨平台方式是什么?

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

I want the simplest possible way to pop up simple dialogs in Python scripts.我想要最简单的方法来在 Python 脚本中弹出简单的对话框。 Ideally, the solution would:理想情况下,解决方案是:

  • Work on Windows, OS X, Gnome, KDE在 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".本质上,您只是在说“弹出带有此文本的标准对话框”,或“弹出带有问题 x 的对话框并将响应输入变量 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.我不想了解 GUI 框架,也不想设置说“启动 GUI 线程、注册事件处理程序、配置一些窗口属性、运行循环”等的代码。我不想设置打开一个窗口或之后关闭窗口。 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然后他们按下两个按钮之一,然后format等于'JPG' ,或者

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' .在他们在框中输入并按 Ok 后, response现在等于'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.我已经列出 Zenity 和 EasyGUI 作为示例答案,因为它们与我想要的相似,但并不完美。

[ Previously asked on Python Forum ] [ 之前在 Python 论坛上问过]

EasyGUI is a single file, and provides a simple way to work with Tkinter dialogs, but they're still ugly non-native Tkinter dialogs. EasyGUI是单个文件,并提供了一种使用 Tkinter 对话框的简单方法,但它们仍然是丑陋的非本地 Tkinter 对话框。

from easygui import msgbox
msgbox('Stuff')

Tkinter 在 Ubuntu 上很丑TKinter 在 Windows 7 上很丑

It can easily be installed using:它可以使用以下方法轻松安装:

$ sudo pip3 install --upgrade easygui

There is a GitHub repository and documentation is very neat.有一个GitHub 存储库文档非常简洁。

Previously, there was also a fork called EasyGuiTtk , which unfortunately is no longer available.以前,还有一个名为EasyGuiTtk的叉子,不幸的是不再可用。

在此处输入图片说明

Zenity works under Linux and Windows , and can be called from Python directly: Zenity工作在 Linux 和Windows 下,可以直接从 Python 调用:

import os
os.system('zenity --info --text="Stuff"')

Using --warning instead of --info gives a warning dialog box instead of an info box.使用--warning而不是--info会给出警告对话框而不是信息框。 Other options can be found here: https://help.gnome.org/users/zenity/stable/其他选项可以在这里找到: https : //help.gnome.org/users/zenity/stable/

The return values from question boxes need to be captured for acting on, though, which is more complex, and you have to learn about communicating with subprocesses, etc.但是,需要捕获来自问题框的返回值以进行操作,但这更复杂,您必须了解如何与子进程通信等。

It can also be used with the PyZenity front-end, which makes capturing return values simple:它还可以与PyZenity前端一起使用,这使得捕获返回值变得简单:

from PyZenity import InfoMessage
InfoMessage('Stuff')

I have tested PyZenity in both Ubuntu and Windows XP, and it works in both.我已经在 Ubuntu 和 Windows XP 中测试了 PyZenity,它在两者中都有效。

Zenity 在 Gnome 中看起来不错令人惊讶的是,Zenity 在 KDE 中看起来也不错Windows 中的 Zenity 有错误的 GTK 主题

I read that Zenity is GTK+ only, but I tried it in Gnome and KDE and it looks native in both.我读到 Zenity 只是 GTK+,但我在 Gnome 和 KDE 中尝试过它,它在两者中看起来都是原生的。 The port to Windows does not look native, though, because it uses the wrong GTK theme?但是,Windows 的端口看起来不是原生的,因为它使用了错误的 GTK 主题?

There are also other programs like KDialog and Xdialog that might be interfaced to a similar Python frontend that could check and see what executables are available so that it automatically takes care of everything?还有其他程序,如KDialogXdialog ,它们可以连接到类似的 Python 前端,可以检查并查看可用的可执行文件,以便它自动处理所有事情? (There's a Ruby frontend for KDialog , too.) (也有用于 KDialogRuby 前端。)

I don't know if PyZenity works under OS X, either.我也不知道 PyZenity 是否在 OS X 下工作。

TkInter is usually supplied with Python TkInter通常与 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如果你想要更原生的东西,你必须安装像 wxpython 这样的东西

The PyMsgBox module does almost exactly this. PyMsgBox 模块几乎就是这样做的。 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).它使用内置的 tkinter 模块作为其遵循 JavaScript 命名约定的消息框函数:alert()、confirm()、prompt() 和 password()(它是 prompt(),但在您键入时使用 *)。 These function calls block until the user clicks an OK/Cancel button.这些函数调用会阻塞,直到用户单击“确定/取消”按钮。 It's a cross-platform, pure Python module with no dependencies.它是一个跨平台的纯 Python 模块,没有依赖项。

Native look-and-feel message boxes will be available in a future version.本机外观消息框将在未来版本中可用。

Install with: pip install PyMsgBox安装方式: 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/完整文档位于http://pymsgbox.readthedocs.org/en/latest/

To extend on endolith's tkMessageBox answer with the ugly empty window in the background...要在后台使用丑陋的空窗口扩展 endolith 的 tkMessageBox 答案...

The code below pops up the box without the background window.下面的代码弹出没有背景窗口的框。

import Tkinter, tkMessageBox
root = Tkinter.Tk()
root.withdraw()
tkMessageBox.showinfo("my dialog title", "my dialog message")

This is lifted directly from a useful comment I found at the bottom of this article .这是直接从一个有用的评论,我发现在底部抬起这篇文章 Thanks to Jason (the commenter) and effbot.org.感谢 Jason(评论者)和 effbot.org。

@ endolith, re: zenity for Windows. @endolith,回复:适用于 Windows 的 Zenity。

Hi,你好,

I repackaged "Zenity for Windows" and included the correct GTK-theme file.我重新打包了“Zenity for Windows”并包含了正确的 GTK 主题文件。 It looks much better now.现在看起来好多了。 :) It is now available for download: http://www.placella.com/software/zenity/ :) 现在可以下载了: http : //www.placella.com/software/zenity/

Screenshot:截屏:

替代文字
(source: placella.com ) (来源: placella.com

Peace, Rouslan和平,鲁斯兰

wxPython is the best Python GUI library (IMO) and uses native widgets. wxPython 是最好的 Python GUI 库 (IMO) 并使用本机小部件。

import wx
app = wx.PySimpleApp()
dialog = wx.MessageDialog(None, 'wxPython is awesome!', 'Dialog Box', wx.OK|wx.ICON_INFORMATION)
dialog.ShowModal()
dialog.Destroy()
app.MainLoop()

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:另一种可能性是tkMessageBox 模块,它显然内置在标准库中并且是跨平台的,尽管这比其他模块更难看:

import tkMessageBox
tkMessageBox.showinfo('Title','Stuff') 

Tkinter 超级丑

pyglet is another alternative, though it may not be the simplest. pyglet 是另一种选择,尽管它可能不是最简单的。 that being said, it's cross-platform and only depends on python, so there's no external dependencies.话虽如此,它是跨平台的,只依赖于 python,所以没有外部依赖。 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()

This is not possible.这不可能。 If you want simple then you have to use Tkinter because that is what is included.如果您想要简单,那么您必须使用 Tkinter,因为这就是其中的内容。 If Tkinter is not good enough then you will have to choose and package a GUI for each platform separately.如果 Tkinter 不够好,那么您将不得不为每个平台分别选择和打包一个 GUI。

I suggest that you do use Tkinter and wrap the parts that you need in a class that will be even simpler to use.我建议您确实使用 Tkinter 并将您需要的部分包装在一个更易于使用的类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM