“Python/docs/3.9/faq/gui”的版本间差异

来自菜鸟教程
Python/docs/3.9/faq/gui
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:图形用户界面常见问题解答 — Python 文档}}
 
<div id="graphic-user-interface-faq" class="section">
 
<div id="graphic-user-interface-faq" class="section">
  
= [[#id3|Graphic User Interface FAQ]] =
+
= 图形用户界面常见问题解答 =
  
<div id="id1" class="contents topic">
+
<div id="contents" class="contents topic">
  
目录
+
内容
  
* [[#graphic-user-interface-faq|Graphic User Interface FAQ]]
+
* [[#graphic-user-interface-faq|图形用户界面常见问题解答]]
** [[#general-gui-questions|General GUI Questions]]
+
** [[#general-gui-questions|一般 GUI 问题]]
** [[#what-platform-independent-gui-toolkits-exist-for-python|What platform-independent GUI toolkits exist for Python?]]
+
** [[#what-gui-toolkits-exist-for-python|Python 有哪些 GUI 工具包?]]
*** [[#tkinter|Tkinter]]
+
** [[#tkinter-questions|Tkinter 问题]]
*** [[#wxwidgets|wxWidgets]]
+
*** [[#how-do-i-freeze-tkinter-applications|如何冻结 Tkinter 应用程序?]]
*** [[#qt|Qt]]
+
*** [[#can-i-have-tk-events-handled-while-waiting-for-i-o|我可以在等待 I/O 时处理 Tk 事件吗?]]
*** [[#gtk|Gtk+]]
+
*** [[#i-can-t-get-key-bindings-to-work-in-tkinter-why|我无法在 Tkinter 中使用键绑定:为什么?]]
*** [[#kivy|Kivy]]
 
*** [[#fltk|FLTK]]
 
*** [[#opengl|OpenGL]]
 
** [[#what-platform-specific-gui-toolkits-exist-for-python|What platform-specific GUI toolkits exist for Python?]]
 
** [[#tkinter-questions|Tkinter questions]]
 
*** [[#how-do-i-freeze-tkinter-applications|How do I freeze Tkinter applications?]]
 
*** [[#can-i-have-tk-events-handled-while-waiting-for-i-o|Can I have Tk events handled while waiting for I/O?]]
 
*** [[#i-can-t-get-key-bindings-to-work-in-tkinter-why|I can't get key bindings to work in Tkinter: why?]]
 
  
  
第27行: 第20行:
 
<div id="general-gui-questions" class="section">
 
<div id="general-gui-questions" class="section">
  
== [[#id4|General GUI Questions]] ==
+
== 一般 GUI 问题 ==
  
  
 
</div>
 
</div>
<div id="what-platform-independent-gui-toolkits-exist-for-python" class="section">
+
<div id="what-gui-toolkits-exist-for-python" class="section">
  
== [[#id5|What platform-independent GUI toolkits exist for Python?]] ==
+
== Python 有哪些 GUI 工具包? ==
  
Depending on what platform(s) you are aiming at, there are several. Some
+
Python 的标准构建包括 Tcl/Tk 小部件集的面向对象接口,称为 [[../../library/tk#tkinter|tkinter]]。 这可能是最容易安装和使用的(因为它包含在 Python 的大多数 [https://www.python.org/downloads/ 二进制发行版] 中)。 有关 Tk 的更多信息,包括指向源的指针,请参阅 [https://www.tcl.tk Tcl/Tk 主页] 。 Tcl/Tk 完全可移植到 macOS、Windows 和 Unix 平台。
of them haven't been ported to Python 3 yet. At least [[#tkinter|Tkinter]] and [[#qt|Qt]]
 
are known to be Python 3-compatible.
 
  
<div id="tkinter" class="section">
+
根据您的目标平台,还有多种选择。 可以在 python wiki 上找到跨平台 [https://wiki.python.org/moin/GuiProgramming#Cross-Platform_Frameworks 和] [https://wiki.python.org/moin/GuiProgramming#Platform-specific_Frameworks 特定于平台的] GUI 框架的 列表。
 
 
=== [[#id6|Tkinter]] ===
 
 
 
Standard builds of Python include an object-oriented interface to the Tcl/Tk
 
widget set, called [[../../library/tk#tkinter|<span class="std std-ref">tkinter</span>]]. This is probably the easiest to
 
install (since it comes included with most
 
[https://www.python.org/downloads/ binary distributions] of Python) and use.
 
For more info about Tk, including pointers to the source, see the
 
[https://www.tcl.tk Tcl/Tk home page]. Tcl/Tk is fully portable to the
 
Mac OS X, Windows, and Unix platforms.
 
  
  
 
</div>
 
</div>
<div id="wxwidgets" class="section">
+
<div id="tkinter-questions" class="section">
  
=== [[#id7|wxWidgets]] ===
+
== Tkinter 问题 ==
  
wxWidgets (https://www.wxwidgets.org) is a free, portable GUI class
+
<div id="how-do-i-freeze-tkinter-applications" class="section">
library written in C++ that provides a native look and feel on a
 
number of platforms, with Windows, Mac OS X, GTK, X11, all listed as
 
current stable targets. Language bindings are available for a number
 
of languages including Python, Perl, Ruby, etc.
 
  
[https://www.wxpython.org wxPython] is the Python binding for
+
=== 如何冻结 Tkinter 应用程序? ===
wxwidgets. While it often lags slightly behind the official wxWidgets
 
releases, it also offers a number of features via pure Python
 
extensions that are not available in other language bindings. There
 
is an active wxPython user and developer community.
 
  
Both wxWidgets and wxPython are free, open source, software with
+
Freeze 是一种创建独立应用程序的工具。 冻结 Tkinter 应用程序时,应用程序不会真正独立,因为应用程序仍需要 Tcl 和 Tk 库。
permissive licences that allow their use in commercial products as
 
well as in freeware or shareware.
 
  
 +
一种解决方案是将应用程序与 Tcl 和 Tk 库一起交付,并在运行时使用 <span id="index-0" class="target"></span><code>TCL_LIBRARY</code> 和 <span id="index-1" class="target"></span><code>TK_LIBRARY</code> 环境指向它们变量。
  
</div>
+
为了获得真正独立的应用程序,构成库的 Tcl 脚本也必须集成到应用程序中。 支持它的一种工具是 SAM(独立模块),它是 Tix 发行版 (http://tix.sourceforge.net/) 的一部分。
<div id="qt" class="section">
 
  
=== [[#id8|Qt]] ===
+
在启用 SAM 的情况下构建 Tix,对 <code>Tclsam_init()</code> 执行适当的调用等。 在 Python 的 <code>Modules/tkappinit.c</code> 中,并与 libtclsam 和 libtksam 链接(您可能还包括 Tix 库)。
 
 
There are bindings available for the Qt toolkit (using either [https://riverbankcomputing.com/software/pyqt/intro PyQt] or [https://wiki.qt.io/PySide PySide]) and for KDE ([https://techbase.kde.org/Languages/Python/Using_PyKDE_4 PyKDE4]).
 
PyQt is currently more mature than PySide, but you must buy a PyQt license from
 
[https://www.riverbankcomputing.com/commercial/license-faq Riverbank Computing]
 
if you want to write proprietary applications. PySide is free for all applications.
 
 
 
Qt 4.5 upwards is licensed under the LGPL license; also, commercial licenses
 
are available from [https://www.qt.io/licensing/ The Qt Company].
 
  
  
 
</div>
 
</div>
<div id="gtk" class="section">
+
<div id="can-i-have-tk-events-handled-while-waiting-for-i-o" class="section">
  
=== [[#id9|Gtk+]] ===
+
=== 我可以在等待 I/O 时处理 Tk 事件吗? ===
  
The [https://wiki.gnome.org/Projects/PyGObject GObject introspection bindings]
+
在 Windows 以外的平台上,是的,您甚至不需要线程! 但是您必须稍微重组您的 I/O 代码。 Tk 与 Xt 的 <code>XtAddInput()</code> 调用等效,它允许您注册一个回调函数,当文件描述符上的 I/O 可能时,它将从 Tk 主循环调用。 请参阅 [[../../library/tkinter#tkinter-file-handlers|文件处理程序]]
for Python allow you to write GTK+ 3 applications. There is also a
 
[https://python-gtk-3-tutorial.readthedocs.io Python GTK+ 3 Tutorial].
 
 
 
The older PyGtk bindings for the [https://www.gtk.org Gtk+ 2 toolkit] have
 
been implemented by James Henstridge; see &lt;http://www.pygtk.org&gt;.
 
  
  
 
</div>
 
</div>
<div id="kivy" class="section">
+
<div id="i-can-t-get-key-bindings-to-work-in-tkinter-why" class="section">
  
=== [[#id10|Kivy]] ===
+
=== 我无法在 Tkinter 中使用键绑定:为什么? ===
  
[https://kivy.org/ Kivy] is a cross-platform GUI library supporting both
+
一个经常听到的抱怨是,即使按下了适当的键,绑定到 <code>bind()</code> 方法的事件的事件处理程序也不会得到处理。
desktop operating systems (Windows, macOS, Linux) and mobile devices (Android,
 
iOS). It is written in Python and Cython, and can use a range of windowing
 
backends.
 
  
Kivy is free and open source software distributed under the MIT license.
+
最常见的原因是绑定应用的小部件没有“键盘焦点”。 查看焦点命令的 Tk 文档。 通常小部件通过点击获得键盘焦点(但不是标签;请参阅 takefocus 选项)。
  
  
 
</div>
 
</div>
<div id="fltk" class="section">
 
 
=== [[#id11|FLTK]] ===
 
 
Python bindings for [http://www.fltk.org the FLTK toolkit], a simple yet
 
powerful and mature cross-platform windowing system, are available from [http://pyfltk.sourceforge.net the
 
PyFLTK project].
 
 
  
 
</div>
 
</div>
<div id="opengl" class="section">
 
 
=== [[#id12|OpenGL]] ===
 
 
For OpenGL bindings, see [http://pyopengl.sourceforge.net PyOpenGL].
 
 
 
</div>
 
 
</div>
 
<div id="what-platform-specific-gui-toolkits-exist-for-python" class="section">
 
 
== [[#id13|What platform-specific GUI toolkits exist for Python?]] ==
 
 
By installing the [https://pypi.org/project/pyobjc/ PyObjc Objective-C bridge], Python programs can use Mac OS X's
 
Cocoa libraries.
 
 
[[../windows#windows-faq|<span class="std std-ref">Pythonwin</span>]] by Mark Hammond includes an interface to the
 
Microsoft Foundation Classes and a Python programming environment
 
that's written mostly in Python using the MFC classes.
 
 
  
 
</div>
 
</div>
<div id="tkinter-questions" class="section">
+
<div class="clearer">
  
== [[#id14|Tkinter questions]] ==
 
  
<div id="how-do-i-freeze-tkinter-applications" class="section">
 
 
=== [[#id15|How do I freeze Tkinter applications?]] ===
 
 
Freeze is a tool to create stand-alone applications. When freezing Tkinter
 
applications, the applications will not be truly stand-alone, as the application
 
will still need the Tcl and Tk libraries.
 
 
One solution is to ship the application with the Tcl and Tk libraries, and point
 
to them at run-time using the <span id="index-0" class="target"></span><code>TCL_LIBRARY</code> and <span id="index-1" class="target"></span><code>TK_LIBRARY</code>
 
environment variables.
 
 
To get truly stand-alone applications, the Tcl scripts that form the library
 
have to be integrated into the application as well. One tool supporting that is
 
SAM (stand-alone modules), which is part of the Tix distribution
 
(http://tix.sourceforge.net/).
 
 
Build Tix with SAM enabled, perform the appropriate call to
 
<code>Tclsam_init()</code>, etc. inside Python's
 
<code>Modules/tkappinit.c</code>, and link with libtclsam and libtksam (you
 
might include the Tix libraries as well).
 
 
 
</div>
 
<div id="can-i-have-tk-events-handled-while-waiting-for-i-o" class="section">
 
 
=== [[#id16|Can I have Tk events handled while waiting for I/O?]] ===
 
 
On platforms other than Windows, yes, and you don't even
 
need threads! But you'll have to restructure your I/O
 
code a bit. Tk has the equivalent of Xt's <code>XtAddInput()</code> call, which allows you
 
to register a callback function which will be called from the Tk mainloop when
 
I/O is possible on a file descriptor. See [[../../library/tkinter#tkinter-file-handlers|<span class="std std-ref">File Handlers</span>]].
 
 
 
</div>
 
<div id="i-can-t-get-key-bindings-to-work-in-tkinter-why" class="section">
 
 
=== [[#id17|I can't get key bindings to work in Tkinter: why?]] ===
 
 
An often-heard complaint is that event handlers bound to events with the
 
<code>bind()</code> method don't get handled even when the appropriate key is pressed.
 
 
The most common cause is that the widget to which the binding applies doesn't
 
have &quot;keyboard focus&quot;. Check out the Tk documentation for the focus command.
 
Usually a widget is given the keyboard focus by clicking in it (but not for
 
labels; see the takefocus option).
 
 
 
</div>
 
 
</div>
 
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:50的最新版本

图形用户界面常见问题解答

一般 GUI 问题

Python 有哪些 GUI 工具包?

Python 的标准构建包括 Tcl/Tk 小部件集的面向对象接口,称为 tkinter。 这可能是最容易安装和使用的(因为它包含在 Python 的大多数 二进制发行版 中)。 有关 Tk 的更多信息,包括指向源的指针,请参阅 Tcl/Tk 主页 。 Tcl/Tk 完全可移植到 macOS、Windows 和 Unix 平台。

根据您的目标平台,还有多种选择。 可以在 python wiki 上找到跨平台 特定于平台的 GUI 框架的 列表。


Tkinter 问题

如何冻结 Tkinter 应用程序?

Freeze 是一种创建独立应用程序的工具。 冻结 Tkinter 应用程序时,应用程序不会真正独立,因为应用程序仍需要 Tcl 和 Tk 库。

一种解决方案是将应用程序与 Tcl 和 Tk 库一起交付,并在运行时使用 TCL_LIBRARYTK_LIBRARY 环境指向它们变量。

为了获得真正独立的应用程序,构成库的 Tcl 脚本也必须集成到应用程序中。 支持它的一种工具是 SAM(独立模块),它是 Tix 发行版 (http://tix.sourceforge.net/) 的一部分。

在启用 SAM 的情况下构建 Tix,对 Tclsam_init() 执行适当的调用等。 在 Python 的 Modules/tkappinit.c 中,并与 libtclsam 和 libtksam 链接(您可能还包括 Tix 库)。


我可以在等待 I/O 时处理 Tk 事件吗?

在 Windows 以外的平台上,是的,您甚至不需要线程! 但是您必须稍微重组您的 I/O 代码。 Tk 与 Xt 的 XtAddInput() 调用等效,它允许您注册一个回调函数,当文件描述符上的 I/O 可能时,它将从 Tk 主循环调用。 请参阅 文件处理程序


我无法在 Tkinter 中使用键绑定:为什么?

一个经常听到的抱怨是,即使按下了适当的键,绑定到 bind() 方法的事件的事件处理程序也不会得到处理。

最常见的原因是绑定应用的小部件没有“键盘焦点”。 查看焦点命令的 Tk 文档。 通常小部件通过点击获得键盘焦点(但不是标签;请参阅 takefocus 选项)。