简体   繁体   English

如何用 wxPython、PyQt、PySide 或 Tkinter 解释 Python?

[英]How is Python interpreted with wxPython, PyQt, PySide or Tkinter?

I got curious and have been reading about GUI development using Python for the past hour.我很好奇,在过去的一个小时里一直在阅读有关使用 Python 进行 GUI 开发的内容。 After reading documentation of wxPython, PyQt, Nokia's Python bindings for Qt along with Tkinter a question came to my mind.在阅读了 wxPython、PyQt、诺基亚针对 Qt 的 Python 绑定以及 Tkinter 的文档后,我想到了一个问题。

When I create a console application with Python, it runs using the embedded Python interpreter (which I assume is usually if not always in my case cpython).当我用 Python 创建一个控制台应用程序时,它使用嵌入式 Python 解释器运行(我假设在我的例子中通常是 cpython,如果不是总是的话)。

So I was wondering, what's the case with these "widget toolkits"?所以我想知道,这些“小部件工具包”是怎么回事?

  • How is the Python code executed and what interprets it (or executed it)? Python 代码是如何执行的,什么解释(或执行)它?

  • Which part of my Python code is interpreted using the Python interpreter?我的 Python 代码的哪一部分是使用 Python 解释器解释的?

  • Or does the Python code get lexically analysed and then parsed by the widget's toolkit which then interpretes and executes (or compile during build)?还是对 Python 代码进行词法分析,然后由小部件的工具包进行解析,然后由小部件的工具包进行解释和执行(或在构建期间编译)?

I am looking forward to understanding what goes on in the background in comparison with Python applications' (a bit simpler to understand) interpretation with the Python interpreter.我期待着了解与 Python 应用程序(更容易理解)与 Python 解释器的解释相比,后台发生了什么。

Thank you.谢谢你。

PS.附言。 To whichever genius thinks that this question deserves to be closed;无论哪个天才认为这个问题都值得结束; A lot of people wonder the internals of external libraries and systems.很多人想知道外部库和系统的内部结构。 Especially those which are not as simple as they look.尤其是那些不像看起来那么简单的。 There currently is not any question explaining this on SE.目前没有任何问题在 SE 上对此进行解释。

This is just a really generalized high-level explanation about "GUI toolkits"...这只是关于“GUI 工具包”的真正概括的高级解释......

Lets say you decide to use the Qt framework.假设您决定使用 Qt 框架。 This framework is written in C++.这个框架是用 C++ 编写的。 There are two different python bindings that can be used, allowing one to write a GUI application in python against the same API as the C++ version.有两种不同的 python 绑定可以使用,允许使用 python 针对与 C++ 版本相同的 API 编写 GUI 应用程序。

The python bindings provide a wrapping around calls into the C++ code. python 绑定提供了对 C++ 代码的环绕调用。 PyQt4 for instance uses sip , while PySide uses shiboken . PyQt4 例如使用sip ,而 PySide 使用shiboken These are just language wrapping tools that take specifications for how to map between the C++ objects and their intended python interface.这些只是语言包装工具,它们采用规范来说明如何在 C++ 对象与其预期的 python 接口之间进行映射。

Ok, so you start using PyQt... All of the code you write has to pass through the python interpreter.好的,那么你开始使用 PyQt ......你编写的所有代码都必须通过 python 解释器。 Some of it may be pure python.其中一些可能是纯 python。 Some of it will call into C++ libs to create things like your widgets.其中一些将调用 C++ 库来创建诸如小部件之类的东西。 In Qt, there will be a C++ pointer associated with the python instance counterpart.在 Qt 中,将有一个 C++ 指针与对应的 Python 实例相关联。

It is the C++ layer that is then communicating with the window manager of your platform, to turn platform-independent API calls into something platform specific, like how to exactly draw a button or menu.然后是 C++ 层与您平台的窗口管理器通信,将平台无关的 API 调用转换为平台特定的东西,比如如何精确地绘制按钮或菜单。

Whether you create a console only or GUI based python application, it all goes through the python interpreter to interpret your python code.无论您创建的是仅控制台还是基于 GUI 的 Python 应用程序,它都会通过 Python 解释器来解释您的 Python 代码。 Something must interpret the python language for you.必须有东西为你解释 python 语言。

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

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