⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index.lxp@lxpwrap=x823_252ehtm.htm

📁 GUI Programming with Python
💻 HTM
字号:
    <table border="0" cellspacing="0" cellpadding="3" width="100%"><tr><td>    <div align="center" id="bldcontent">      <a href="../default.htm"><img src="../images/opendocs.png" width="63" height="76" border="0"></a>      <br>      <div class="symbol">Your OpenSource Publisher&#153;</div>    </div>      </td></tr></table>    <div align="center" class="author">      	<a href="../products.lxp">Products</a>	&nbsp;|&nbsp;	<a href="../wheretobuy.lxp">Where to buy</a>	&nbsp;|&nbsp;	<a href="../bookstore.lxp">Retailers</a>	&nbsp;|&nbsp;	<a href="../faq.lxp">FAQ</a>	&nbsp;|&nbsp;        <a href="../writeforus.lxp">Write for Us.</a>        &nbsp;|&nbsp;        <a href="#contact">Contact Us.</a>  </div>    <table border="0" cellspacing="3" cellpadding="0" width="100%"><tr><td width="100%">      <div class="content">        <table border="0" cellspacing="2" cellpadding="0" width="100%"><tr><td width="100%">          <div align="center"><H4 CLASS="AUTHOR"><A NAME="AEN5">Boudewijn Rempt</A><br><a href="../../https@secure.linuxports.com/opendocs/default.htm"><img src=odpyqt125.png></a><br>ISBN: 0-97003300-4-4<br><a href="../../https@secure.linuxports.com/opendocs/default.htm">Available from bookstores everywhere or you can order it here.</a><p>You can download the source files for the book <a href="pyqtsrc.tgz">(code / eps) here.</a><hr></div>                    <HTML><HEAD><TITLE>Constructions</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.72"><LINKREL="HOME"TITLE="GUI Programming with Python: QT Edition"HREF="book1.htm"><LINKREL="UP"TITLE="Introduction to Python"HREF="c653.htm"><LINKREL="PREVIOUS"TITLE="The Rules"HREF="x719.htm"><LINKREL="NEXT"TITLE="Conclusion"HREF="x879.htm"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GUI Programming with Python: QT Edition</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><A accesskey="P" href="index.lxp@lxpwrap=x719_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 4. Introduction to Python</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x879_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Constructions</A></H1><P>Python, like all languages, gives you      constructions for looping, branching and jumping. In addition,      since Python 2.2, you can also use iterators and      generators.</P><DIVCLASS="SECT2"><H2CLASS="SECT2">Looping</A></H2><P>You do not use counters to loop in        Python. Rather, you use sequences of objects to loop over.        Those objects can of course also be be numbers, generated by        either <TTCLASS="FUNCTION">range</TT> or        <TTCLASS="FUNCTION">xrange</TT>:</P><PRECLASS="SCREEN">Python 2.1.1 (#1, Aug 11 2001, 20:14:53)[GCC 2.95.2 19991024 (release)] on linux2Type "copyright", "credits" or "license" for more information.&#62;&#62;&#62; aList=["a","b","c"]&#62;&#62;&#62; for item in aList:...     print item...abc&#62;&#62;&#62; for counter in range(3):...     print counter...012&#62;&#62;&#62;      </PRE><P>Another loop repeats a block of        statements while a certain expression evaluates to        true:</P><PRECLASS="SCREEN">&#62;&#62;&#62; a=0&#62;&#62;&#62; while a &#60; 3:...     print a...     a+=1...012      </PRE><P>The <TTCLASS="FUNCTION">break</TT> statement        breaks execution out of a loop; the        <TTCLASS="FUNCTION">continue</TT> statement continues immediately        with the next iteration.</P><P>Iterators define a <TTCLASS="FUNCTION">__iter__</TT> and a        <TTCLASS="FUNCTION">next()</TT> function to allow easy        looping:</P><PRECLASS="PROGRAMLISTING"># iter.py - an iteratorclass MyIterator:    def __init__(self, start):        self.start = start    def __iter__(self):        return self    def next(self):        if self.start &#60; 10:            self.start += 1            return self.start        else:            raise StopIterationfor i in MyIterator(1):    print i      </PRE><P>Generators are functions that return a function that can        yield a result part-way compution, and resume later:</P><PRECLASS="PROGRAMLISTING"># generator.pyfrom __future__ import generatorsdef MyGenerator():    count = 0    while count &#60; 10:        yield count        count += 1gen = MyGenerator()try:    while 1:        print gen.next()except StopIteration:    print "finished"      </PRE><P>Note how <TTCLASS="FUNCTION">yield</TT> returns the number,        but <TTCLASS="VARNAME">count</TT> is still increased.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Branching</A></H2><P>The number zero, empty lists,        dictionaries, tuples and the object <TTCLASS="VARNAME">None</TT>        all evaluate to false; (almost) everything else is        true. You create branches using the <TTCLASS="FUNCTION">if</TT>        statement.</P><PRECLASS="SCREEN">&#62;&#62;&#62; a=1&#62;&#62;&#62; if a:...     print "true"... else:...     print "false"...true&#62;&#62;&#62; if a==0:...     print "a was zero"... elif a == None:...     print "a was none"... else:...     print "a was zero nor none"...a was zero nor none      </PRE><P>The operator <TTCLASS="FUNCTION">==</TT>        tests for equality, while <TTCLASS="FUNCTION">!=</TT> (or the        deprecated <TTCLASS="FUNCTION">&#60;&#62;</TT>) tests for        inequality. The operator <TTCLASS="FUNCTION">is</TT> tests for        identity: that is, whether two references point to (unless you        <TTCLASS="LITERAL">import division from future</TT> in Python 2.2)        the same object:</P><PRECLASS="SCREEN">&#62;&#62;&#62; from qt import *&#62;&#62;&#62; a=QString("bla")&#62;&#62;&#62; b=QString("bla")&#62;&#62;&#62; a is b0&#62;&#62;&#62; c=a&#62;&#62;&#62; a is c1&#62;&#62;&#62; a="bla"&#62;&#62;&#62; b="bla"&#62;&#62;&#62; a is b1&#62;&#62;&#62; id(a)135455928&#62;&#62;&#62; id(b)135455928      </PRE><P>As you can see, Python does some        optimizations that reuse the same string object if the string        contents are the same.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Exceptions</A></H2><P>As every modern programming language        must have, Python contains an error catching construction.        This is the try: ... except... construction.</P><PRECLASS="SCREEN">&#62;&#62;&#62; try:...     1/0... except ZeroDivisionError:...     print "Zerodivisionerror"...Zerodivisionerror      </PRE><P>You can also create your own exceptions        that can carry significant data about the causes of the        error:</P><PRECLASS="SCREEN">&#62;&#62;&#62; class BlaError:...     def __init__(self, value):...             self.value = value...     def __str__(self):...             return repr(self.value)&#62;&#62;&#62; try:...     raise BlaError("Bla happened - that's bad!")... except BlaError, error:...     print error...Bla happened - that's bad!      </PRE><P>If you want to catch several different exceptions, you        have to create a tuple of all the exceptions you want to        catch:</P><PRECLASS="SCREEN">&#62;&#62;&#62; try:...     print "bla"... except (ValueError, ZeroDivisionError):...     print "that's bad"... bla      </PRE><P>Finally, you can define something that        should happen when all errors have been handled in the        <TTCLASS="FUNCTION">finally</TT> block:</P><PRECLASS="SCREEN">&#62;&#62;&#62; try:...     1/0... finally:...     print "finally"...finallyTraceback (most recent call last):  File "&#60;stdin&#62;", line 2, in ?ZeroDivisionError: integer division or modulo by zero      </PRE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Classes</A></H2><P>Classes are defined with the        <TTCLASS="FUNCTION">class</TT> keyword. Python classes can inherit        from zero, one, or more other classes, but from only one PyQt        class.</P><P>Classes are initialized using the code        in the <TTCLASS="FUNCTION">__init__</TT> method. There are other        special methods, like <TTCLASS="FUNCTION">__str__</TT>, which        should return a string representation of the class. Consult        the Python language reference for a complete list of        these.</P><PRECLASS="SCREEN">&#62;&#62;&#62;class A:pass...&#62;&#62;&#62; class B(A):...     def __init__(self, val):...             self.val = val...     def __str__(self):...             return str(self.val)...&#62;&#62;&#62; b=B(10)&#62;&#62;&#62; print b10&#62;&#62;&#62;      </PRE></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><A accesskey="P" href="index.lxp@lxpwrap=x719_252ehtm.htm">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="H" href="index.lxp@lxpwrap=book1_252ehtm">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><A accesskey="N" href="index.lxp@lxpwrap=x879_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">The Rules</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c653_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Conclusion</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -