简体   繁体   English

从python GUI使用C编译代码

[英]Using C compiled code from python GUI

I am writing an application where I am coding all of my front-end and GUI with python library (wxpython specifically). 我正在编写一个应用程序,在其中使用python库(特别是wxpython)对所有前端和GUI进行编码。 For this application, I would like to write the model class with C and use python to use the compiled C code? 对于此应用程序,我想用C编写模型类,并使用python来使用编译后的C代码? How can this be implemented in python? 如何在python中实现呢?

I know this is little vague question but I am struggling with the starting point. 我知道这不是一个模糊的问题,但我正在为起点而苦苦挣扎。

If you're using CPython (the most popular version of Python), you'll need to learn the CPython C API . 如果您使用的是CPython(最流行的Python版本),则需要学习CPython C API Other python implementations may or may not support C calls. 其他python实现可能支持C调用,也可能不支持。 You can also use the ctypes library which is easier to learn, but also more rigid and may not support everything you need. 您还可以使用ctypes库,它更易于学习,但也更加严格,可能无法支持您所需的一切。

您可以尝试Cython ,它基本上是Python,具有对C类型和调用的嵌入式支持(通过CPython API进行)。

You could strictly seperate design(python part) and code(c++ part) like this: 您可以将设计(python部分)和代码(c ++部分)严格分开,如下所示:

Write a complete c++ programm that works in the terminal/console and then make the python-application call these c++-terminal programm via os.Popen. 编写一个可以在终端/控制台上运行的完整c ++程序,然后使python应用程序通过os.Popen调用这些c ++-terminal程序。

So if your programm is a calculator it does this: 因此,如果您的程序是一个计算器,它将执行以下操作:

(python gui) 5 + 5 -> my_c_programm.exe "5 + 5" -> (returns) 10 -> (python gui) display (python gui)5 + 5-> my_c_programm.exe“ 5 + 5”->(返回)10->(python gui)显示

that way you can use your programm with and without gui. 这样,您可以在有和没有gui的情况下使用您的程序。

Its easier and faster than embedding Python in your c++ programm or extending Python with C++. 它比在您的c ++程序中嵌入Python或用C ++扩展Python更加容易和快捷。

I basically do the same thing on my current project, but like this: 我基本上在当前项目中做同样的事情,但是像这样:

php: my webinterface python: for structure and logic and easy operation c++: for heavy calculations and where I need speed php:我的网络界面python:用于结构和逻辑以及易于操作的c ++:用于繁重的计算以及需要提高速度的地方

so php -> python -> c++ 所以php-> python-> c ++

and it works very well for me :) 它对我来说非常好:)

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

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