<!DOCTYPE html><html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>QPython’s main features — QPython 0.9 documentation</title><link rel="stylesheet" href="../static/qpython_theme.css" type="text/css" /><link rel="stylesheet" href="../static/pygments.css" type="text/css" /><script type="text/javascript">var DOCUMENTATION_OPTIONS = {URL_ROOT: '../',VERSION: '0.9',COLLAPSE_INDEX: false,FILE_SUFFIX: '.html',HAS_SOURCE: true,SOURCELINK_SUFFIX: '.txt'};</script><script type="text/javascript" src="../static/jquery.js"></script><script type="text/javascript" src="../static/underscore.js"></script><script type="text/javascript" src="../static/doctools.js"></script><link rel="index" title="Index" href="../genindex.html" /><link rel="search" title="Search" href="../search.html" /><link rel="next" title="Use the best way for developing" href="guide_ide.html" /><link rel="prev" title="Writing "Hello World"" href="guide_helloworld.html" /><meta charset="UTF-8"><link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"><script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no"></head><body><div class="header doc-header"><div class="col-lg-offset-2 col-lg-8 col-md-offset-2 col-md-8 col-sm-offset-2 col-sm-8 index-header"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse"data-target="#example-navbar-collapse"><span class="sr-only">切换导航</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><a href="/index.html" class="logo"><img src="../static/img_logo.png"></a></div><ul class="header-title hidden-xs"><li class="header-phone-selected"><a href="/index.html">Home</a></li><li><a href="/document.html" class="header-selected">Guide</a><span class="header-title-selected"></span></li><li><a href="//edu.qpython.org/">Course</a></li><li><a href="//www.aipy.org">AIPY</a></li><li><a target="_blank" href="//github.com/qpython-android/qpython">Github</a></li></ul><div class="search-box hidden-xs"><form action="../search.html"><input type="search" name="q" placeholder="keyword"><input type="hidden" name="check_keywords" value="yes" /><input type="hidden" name="area" value="default" /><button><img src="../static/ic_search.png"></button></form></div></div></div><div class="row doc-content"><div class="content-main col-sm-offset-2 col-sm-8"><div class="content-wrapper col-sm-4"><div class="left-menu"><div class="doc-title hidden-xs">QPython</div><div class="sidebar collapse navbar-collapse" id="example-navbar-collapse"><ul class="current"><li class="toctree-l1"><a class="reference internal" href="guide.html">Getting started</a></li><li class="toctree-l1 current"><a class="reference internal" href="guide.html#programming-guide">Programming Guide</a><ul class="current"><li class="toctree-l2 current"><a class="current reference internal" href="#">QPython’s main features</a><ul><li class="toctree-l3"><a class="reference internal" href="#why-should-i-choose-qpython">Why should I choose QPython</a></li><li class="toctree-l3"><a class="reference internal" href="#id1">QPython’s main features</a></li></ul></li><li class="toctree-l2"><a class="reference internal" href="guide_ide.html">Use the best way for developing</a></li><li class="toctree-l2"><a class="reference internal" href="guide_libraries.html">QPython built-in Libraries</a></li><li class="toctree-l2"><a class="reference internal" href="guide_libraries.html#python-3rd-libraries">Python 3rd Libraries</a></li><li class="toctree-l2"><a class="reference internal" href="guide_libraries.html#androidhelper-apis">Androidhelper APIs</a></li><li class="toctree-l2"><a class="reference internal" href="guide_extend.html">QPython Open API</a></li><li class="toctree-l2"><a class="reference internal" href="guide_extend.html#qpython-online-service">QPython Online Service</a></li></ul></li><li class="toctree-l1"><a class="reference internal" href="guide.html#developer-guide">Developer Guide</a></li><li class="toctree-l1"><a class="reference internal" href="guide.html#contributor-guide">Contributor Guide</a></li></ul><ul><li class="toctree-l1"><a class="reference internal" href="faq.html">FAQ</a></li></ul><ul><li class="toctree-l1"><a class="reference internal" href="../zhindex.html">中文用户向导</a></li></ul><div class="search-phone visible-xs"><form action="../search.html"><input type="search" name="q" placeholder="keyword"><input type="hidden" name="check_keywords" value="yes" /><input type="hidden" name="area" value="default" /><button><img src="../static/ic_search.png"></button></form></div></div></div></div><div class="content col-sm-8"><div class="document"><ul class="crumbs"><li><a href="../document.html">Guide</a> »</li><li><a href="guide.html">Getting started</a> »</li><li>QPython’s main features</li></ul><div class="documentwrapper"><div class="bodywrapper"><div class="body" role="main"><div class="section" id="qpython-s-main-features"><h1>QPython’s main features<a class="headerlink" href="#qpython-s-main-features" title="Permalink to this headline">¶</a></h1><p><strong>With QPython, you could build android applications with android application and script language now.</strong></p><div class="section" id="why-should-i-choose-qpython"><h2>Why should I choose QPython<a class="headerlink" href="#why-should-i-choose-qpython" title="Permalink to this headline">¶</a></h2><p>The smartphone is becomming people’s essential information & technical assitant, so an flexiable script engine could help people complete most jobs efficiently without complex development.</p><p>QPython offer <strong>an amazing developing experience</strong>, with it’s help, you could implement the program easily without complex installing IDE, compiling, package progress etc.</p></div><div class="section" id="id1"><h2>QPython’s main features<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2><p>You can do most jobs through QPython just like the way that Python does on PC/Laptop.</p><p><strong>Libraries</strong></p><ul class="simple"><li>QPython supports most stardard Python libraries.</li><li>QPython supports many 3rd Python libraries which implemented with pure Python code.</li><li>QPython supports some Python libraries mixed with C/C++ code which pre-compiled by QPython develop team.</li><li>QPython allows you put on the libraries by yourself.</li></ul><p>Besides these, QPython offers some extra features which Python doesn’t offer, Like:</p><ul class="simple"><li>Android APIs Access(Like SMS, GPS, NFC, BLUETOOTH etc)</li></ul><p><em>Why QPython require so many permissions?</em></p><p>QPython need these permissions to access Android’s API.</p><p><strong>Runtime modes</strong></p><p>QPython supports several runtime modes for android.</p><p><strong>Console mode</strong></p><p>It’s the default runtime mode in QPython, it’s very common in PC/laptop.</p><p><strong>Kivy mode</strong></p><p>QPython supports <a class="reference external" href="http://kivy.org">Kivy</a> as the GUI programming solution.</p><p>Kivy is an open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.</p><p>Your device should support opengl2.0 for supporting kivy mode.</p><p>By insert into the following header in your script, you can let your script run with kivy mode.</p><div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1">#qpy:kivy</span></pre></div></div><p><em>An kivy program in QPython sample</em></p><div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1">#qpy:kivy</span><span class="kn">from</span> <span class="nn">kivy.app</span> <span class="k">import</span> <span class="n">App</span><span class="kn">from</span> <span class="nn">kivy.uix.button</span> <span class="k">import</span> <span class="n">Button</span><span class="k">class</span> <span class="nc">TestApp</span><span class="p">(</span><span class="n">App</span><span class="p">):</span><span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span><span class="k">return</span> <span class="n">Button</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s1">'Hello World'</span><span class="p">)</span><span class="n">TestApp</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">()</span></pre></div></div><p>If your library require the opengl driver, you shoule declare the kivy mode header in your script, like the jnius.</p><p><em>NOTE: QPython3 didn’t support kivy mode yet, we have plan to support it in the future</em></p><p><strong>WebApp mode</strong></p><p>We recommend you implement WebApp with QPython for it offer the easy to accomplish UI and Take advantage of Python’s fast programming strong point.</p><p>WebApp will start a webview in front, and run a python web service background.You could use <em>bottle*(QPython built-in library) to implement the web service, or you could install *django</em> / <em>flask</em> framework also.</p><p>By insert into the following header in your script, you can let your script run with webapp mode.</p><div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1">#qpy:webapp:<app title></span><span class="c1">#qpy:<fullscreen or remove this line></span><span class="c1">#qpy://<ip:port:path></span></pre></div></div><p>For example</p><div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1">#qpy:webapp:Hello QPython</span><span class="c1">#qpy://localhost:8080/hello</span></pre></div></div><p>The previous should start a webview which should load the <em>http://localhost:8080/hello</em> as the default page, and the webview will keep the titlebar which title is "Hello QPython", if you add the <em>#qpy:fullscreen</em> it will hide the titlebar.</p><div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1">#qpy:webapp:Hello Qpython</span><span class="c1">#qpy://127.0.0.1:8080/</span><span class="sd">"""</span><span class="sd">This is a sample for qpython webapp</span><span class="sd">"""</span><span class="kn">from</span> <span class="nn">bottle</span> <span class="k">import</span> <span class="n">Bottle</span><span class="p">,</span> <span class="n">ServerAdapter</span><span class="kn">from</span> <span class="nn">bottle</span> <span class="k">import</span> <span class="n">run</span><span class="p">,</span> <span class="n">debug</span><span class="p">,</span> <span class="n">route</span><span class="p">,</span> <span class="n">error</span><span class="p">,</span> <span class="n">static_file</span><span class="p">,</span> <span class="n">template</span><span class="c1">######### QPYTHON WEB SERVER ###############</span><span class="k">class</span> <span class="nc">MyWSGIRefServer</span><span class="p">(</span><span class="n">ServerAdapter</span><span class="p">):</span><span class="n">server</span> <span class="o">=</span> <span class="kc">None</span><span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">handler</span><span class="p">):</span><span class="kn">from</span> <span class="nn">wsgiref.simple_server</span> <span class="k">import</span> <span class="n">make_server</span><span class="p">,</span> <span class="n">WSGIRequestHandler</span><span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">quiet</span><span class="p">:</span><span class="k">class</span> <span class="nc">QuietHandler</span><span class="p">(</span><span class="n">WSGIRequestHandler</span><span class="p">):</span><span class="k">def</span> <span class="nf">log_request</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span> <span class="k">pass</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="s1">'handler_class'</span><span class="p">]</span> <span class="o">=</span> <span class="n">QuietHandler</span><span class="bp">self</span><span class="o">.</span><span class="n">server</span> <span class="o">=</span> <span class="n">make_server</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">host</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">port</span><span class="p">,</span> <span class="n">handler</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">)</span><span class="bp">self</span><span class="o">.</span><span class="n">server</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span><span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span><span class="c1">#sys.stderr.close()</span><span class="kn">import</span> <span class="nn">threading</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">server</span><span class="o">.</span><span class="n">shutdown</span><span class="p">)</span><span class="o">.</span><span class="n">start</span><span class="p">()</span><span class="c1">#self.server.shutdown()</span><span class="bp">self</span><span class="o">.</span><span class="n">server</span><span class="o">.</span><span class="n">server_close</span><span class="p">()</span> <span class="c1">#<--- alternative but causes bad fd exception</span><span class="nb">print</span> <span class="s2">"# qpyhttpd stop"</span><span class="c1">######### BUILT-IN ROUTERS ###############</span><span class="nd">@route</span><span class="p">(</span><span class="s1">'/__exit'</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="p">[</span><span class="s1">'GET'</span><span class="p">,</span><span class="s1">'HEAD'</span><span class="p">])</span><span class="k">def</span> <span class="nf">__exit</span><span class="p">():</span><span class="k">global</span> <span class="n">server</span><span class="n">server</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span><span class="nd">@route</span><span class="p">(</span><span class="s1">'/assets/<filepath:path>'</span><span class="p">)</span><span class="k">def</span> <span class="nf">serverstatic</span><span class="p">(</span><span class="n">filepath</span><span class="p">):</span><span class="k">return</span> <span class="n">static_file</span><span class="p">(</span><span class="n">filepath</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="s1">'/sdcard'</span><span class="p">)</span><span class="c1">######### WEBAPP ROUTERS ###############</span><span class="nd">@route</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span><span class="k">def</span> <span class="nf">home</span><span class="p">():</span><span class="k">return</span> <span class="n">template</span><span class="p">(</span><span class="s1">'<h1>Hello {{name}} !</h1>'</span><span class="o">+</span> \<span class="s1">'<a href="/assets/qpython/projects/WebApp Sample/main.py">View source</a>'</span><span class="p">,</span><span class="n">name</span><span class="o">=</span><span class="s1">'QPython'</span><span class="p">)</span><span class="c1">######### WEBAPP ROUTERS ###############</span><span class="n">app</span> <span class="o">=</span> <span class="n">Bottle</span><span class="p">()</span><span class="n">app</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s1">'/'</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">'GET'</span><span class="p">)(</span><span class="n">home</span><span class="p">)</span><span class="n">app</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s1">'/__exit'</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="p">[</span><span class="s1">'GET'</span><span class="p">,</span><span class="s1">'HEAD'</span><span class="p">])(</span><span class="n">__exit</span><span class="p">)</span><span class="n">app</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s1">'/assets/<filepath:path>'</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">'GET'</span><span class="p">)(</span><span class="n">serverstatic</span><span class="p">)</span><span class="k">try</span><span class="p">:</span><span class="n">server</span> <span class="o">=</span> <span class="n">MyWSGIRefServer</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="s2">"127.0.0.1"</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="s2">"8080"</span><span class="p">)</span><span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">server</span><span class="o">=</span><span class="n">server</span><span class="p">,</span><span class="n">reloader</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span><span class="n">ex</span><span class="p">:</span><span class="nb">print</span> <span class="s2">"Exception: </span><span class="si">%s</span><span class="s2">"</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">ex</span><span class="p">)</span></pre></div></div><p>If you want the webapp could be close when you exit the webview, you have to define the <em>@route(‘/__exit’, method=[‘GET’,’HEAD’])</em> method , for the qpython will request the <em>http://localhost:8080/__exit</em> when you exit the webview. So you can release other resource in this function.</p><img alt="QPython WebApp Sample" src="../images/guide_program_pic1.png" /><p><em>Running screenshot</em></p><p>In the other part of the code, you could implement a webserver whish serve on localhost:8080 and make the URL /hello implement as your webapp’s homepage.</p><p><strong>Q mode</strong></p><p>If you don’t want the QPython display some UI, pelase try to use the QScript mode, it could run a script background, just insert the following header into your script:</p><div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1">#qpy:qpyapp</span></pre></div></div></div></div></div></div></div></div><div class="fb-comments" data-href="http://qpython.org" data-numposts="10"></div></div></div><div class="col-sm-2"></div></div><footer id="footer"><div class="col-sm-offset-2 col-sm-6 footer-div1"><div class="footer-block-item"><span class="footer-item1">Built with Sphinx using a theme provid by QPython.</span><span class="footer-item2 visible-xs">© Copyright 2017, QPython.</span></div></div><div class="col-sm-2 footer-div2"><a href="/index.html" class="pull-right"><img src="../static/img_logo.png"></a></div></footer><script type="text/javascript">$('.fb-comments').attr('data-href', (window.location.href.split('#')[0]).split('?')[0])(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return;js = d.createElement(s); js.id = id;js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.8&appId=1680815145503311";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));$(function(){if ($('#footer')[0].offsetTop < $(window).height()){$('#footer').addClass('to-bottom')}})</script></body></html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。