如何在 Python 中编写 GUI?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2310130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:20:21  来源:igfitidea点击:

How to write GUI in Python?

pythonuser-interface

提问by Roman

I would like to try to write a GUI application in Python. I found out that there are a lot of ways to do it (different toolkits). And, in this context, I have several basic (and I think simple) question?

我想尝试用 Python 编写一个 GUI 应用程序。我发现有很多方法可以做到(不同的工具包)。而且,在这种情况下,我有几个基本(我认为很简单)的问题?

  1. Is it, in general, a good idea to write a GUI application in Python?

  2. What is the standard (easiest and most stable) way to create a GUI applications in Python?

  3. Does anybody can give me a link to a simple Hello World GUI application written in Python?

  1. 一般来说,用 Python 编写 GUI 应用程序是个好主意吗?

  2. 在 Python 中创建 GUI 应用程序的标准(最简单和最稳定)方法是什么?

  3. 谁能给我一个用 Python 编写的简单 Hello World GUI 应用程序的链接?

回答by Yuval Adam

  1. Depends on what application you are writing. I would use Python for a simple GUI, yes.
  2. Use a proper toolkit (such as PyQt- Python bindings for the popular Qt)
  3. Sure
  1. 取决于您正在编写的应用程序。我会使用 Python 来创建一个简单的 GUI,是的。
  2. 使用合适的工具包(例如PyQt- 流行 Qt 的 Python 绑定)
  3. 当然

Hello world in PyQt:

PyQt 中的你好世界:

import qt,sys

a = qt.QApplication(sys.argv)
w = qt.QPushButton("Hello World",None)

a.setMainWidget(w)
w.show()
a.exec_loop()

回答by Jon

I am really fond of pygtk and glade. Pygtk is a python binding for gtk, the gui toolkit used in gnome. Glade is a user interface designer which stores a gui as xml, which can be loaded in pygtk.

我真的很喜欢pygtk和glade。Pygtk 是 gtk 的 Python 绑定,gtk 是 gnome 中使用的 gui 工具包。Glade 是一个用户界面设计器,它将 gui 存储为 xml,可以在 pygtk 中加载。

If you want to see some example code, you can take a look at my project https://launchpad.net/pumped. Just download the source.

如果您想查看一些示例代码,可以查看我的项目https://launchpad.net/pumped。直接下载源码。

回答by seanhodges

If you're looking to make a fairly simple GUI, then PyGTK is extremely easy to use:

如果您想制作一个相当简单的 GUI,那么 PyGTK 非常易于使用:

http://www.pygtk.org/

http://www.pygtk.org/

A tutorial (with downloadable sample code) can be found here, and another one on the Wiki.

可以在此处找到教程(带有可下载的示例代码),在Wiki上找到另一个教程。

回答by ConcernedOfTunbridgeWells

As an answer for #1: Yes. It is quite good for this; scripting languages with GUI toolkits are often a good way to put a GUI on an application. They can also be used to wrap applications written in low level languages such as C or C++. Python offers good integration to quite a few toolkits. The posting linked above gives a pretty good cross section of the options with code samples.

作为#1 的答案:是的。这一点很好;带有 GUI 工具包的脚本语言通常是将 GUI 放在应用程序上的好方法。它们还可用于包装用 C 或 C++ 等低级语言编写的应用程序。Python 为相当多的工具包提供了良好的集成。上面链接的帖子提供了带有代码示例的选项的相当不错的横截面。

For #2: TkInter comes with the standard distribution. It is easy to use but not as sophisticated as (say) QT or WxWidgets.

对于#2:TkInter 带有标准发行版。它易于使用,但不像(比如)QT 或 WxWidgets 那样复杂。

回答by james parsons

python is ok for gui better than perl and ruby heres some tk

python对于gui来说比perl更好,ruby继承了一些tk

from tkinter import *
from tkinter import ttk
root = Tk()
ttk.Button(root, text="Hello World").grid()
root.mainloop()

回答by Noctis Skytower

If you want to learn some GUI programming in Python using Tkinter, you can see a step-by-step process of building a simple MineSweep clone in twelve progressions here: MineSweep for Python 3.x

如果您想使用 Tkinter 在 Python 中学习一些 GUI 编程,您可以在此处查看以十二个进程构建简单 MineSweep 克隆的分步过程:MineSweep for Python 3.x