index.lxp@lxpwrap=x1067_252ehtm.htm

来自「GUI Programming with Python」· HTM 代码 · 共 520 行

HTM
520
字号
    <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>      As simple as they come    </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="Qt Concepts"HREF="c1036.htm"><LINKREL="PREVIOUS"TITLE="Qt Concepts"HREF="c1036.htm"><LINKREL="NEXT"TITLE="      A better Hello World    "HREF="x1149.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=c1036_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 6. Qt Concepts</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x1149_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">As simple as they come</A></H1><P>Nothing like getting the feet wet: let's investigate the      structure of a PyQt application by putting together the minimal      set of components that can show something on screen, slowly      expanding it to show more features.</P><P>A tiny PyQt applications has the following elements:</P><P></P><UL><LI><P>          an application object        </P></LI><LI><P>          a main window (which has a central widget), <SPAN><ICLASS="EMPHASIS">or</I></SPAN>        </P></LI><LI><P>          a main widget        </P></LI></UL><P>This is the traditional &#8221;Hello World&#8221; button      application, with as little code as possible:</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Hello World</P></DIV></P></DIV><DIVCLASS="EXAMPLE"></A><P><B>Example 6-1. hello1.py &#8212; hello world</B></P><PRECLASS="PROGRAMLISTING">## hello1.py#import sys                                                 <IMGSRC="images/callouts/1.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(1)"></A>from qt import *                                           <IMGSRC="images/callouts/2.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(2)"></A>app=QApplication(sys.argv)                                 <IMGSRC="images/callouts/3.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(3)"></A>button=QPushButton("Hello World", None)                    <IMGSRC="images/callouts/4.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(4)"></A>app.setMainWidget(button)                                  <IMGSRC="images/callouts/5.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(5)"></A>button.show()                                              <IMGSRC="images/callouts/6.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(6)"></A>app.exec_loop()                                            <IMGSRC="images/callouts/7.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(7)"></A>        </PRE><DIVCLASS="CALLOUTLIST"><DLCOMPACT="COMPACT"><DT><A href="index.lxp@lxpwrap=x1067_252ehtm.htm#HELLO1IMPSYS"><IMGSRC="images/callouts/1.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(1)"></A></DT><DD>We need to import the Python              <SPAN><ICLASS="EMPHASIS">sys</I></SPAN> package, because the              <TTCLASS="CLASSNAME">QApplication</TT> object wants to look              at the command-line arguments the script is started              with. For instance, starting the script with              <TTCLASS="USERINPUT"><B>python hello1.py -style=platinum</B></TT>              starts the script with the look and feel of Mac-OS 8.5,              by passing the &#8220;-style=platinum&#8221; option              through to the QApplication object.</DD><DD><P>One of the niceties of Qt is that              you have access to all supported widget styles on all              platforms. (Except for the Aqua style - that is only              available on OS X, because Apple doesn't want it to              spread to other platforms.)</P></DD><DT><A href="index.lxp@lxpwrap=x1067_252ehtm.htm#HELLO1IMPQT"><IMGSRC="images/callouts/2.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(2)"></A></DT><DD>Next, we have to import the qt              library. While it is possible to import only explicitly              the elements from the library we need, it's just as easy              and efficient to import the whole library. But we could              have said: <PRECLASS="PROGRAMLISTING"> from qt import QApplication,                QPushButton </PRE>            </DD><DD><P>From version 3.x of PyQt, the              library has been split into several separate modules.              The <TTCLASS="VARNAME">Qt</TT> module still gets you all the              basic stuff, but more advanced functionality, such as              the canvas, is divided over separate modules,              <TTCLASS="VARNAME">qtcanvas</TT> for              <TTCLASS="CLASSNAME">QCanvas</TT>, for instance.</P></DD><DT><A href="index.lxp@lxpwrap=x1067_252ehtm.htm#HELLO1CREATEAPP"><IMGSRC="images/callouts/3.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(3)"></A></DT><DD>After importing the necessary              modules, we create a Qt application object. This object              handles the dispatching of events from the mouse and              keyboard to the various widgets of the application.              Never try to create more than one              <TTCLASS="CLASSNAME">QApplication</TT> object, not even if              you embed Python and PyQt in a C++ Qt application. In              that case, the C++ application should create the              <TTCLASS="CLASSNAME">QApplication object</TT> and pass a              reference to the embedded Python interpreter.</DD><DT><A href="index.lxp@lxpwrap=x1067_252ehtm.htm#HELLO1BUTTON"><IMGSRC="images/callouts/4.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(4)"></A></DT><DD>To keep things simple, we do not create a separate              window object, but rather simply a pushbutton, of the type              <TTCLASS="CLASSNAME">QPushButton</TT>. The first argument              to the creation of the              <TTCLASS="CLASSNAME">QPushButton</TT> is the text that is              shown on the button. Since this is the only widget of              the application, it doesn't have a parent.  This is what              the <TTCLASS="VARNAME">None</TT> argument means &#8212;              there is no parent, and the              <TTCLASS="CLASSNAME">QPushButton</TT> is the root of the              application widget tree.</DD><DT><A href="index.lxp@lxpwrap=x1067_252ehtm.htm#HELLO1SETMAINWIDGET"><IMGSRC="images/callouts/5.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(5)"></A></DT><DD>However, we still need to apprise              the <TTCLASS="CLASSNAME">QApplication</TT> object of that              fact &#8212; this is done by telling the              <TTCLASS="CLASSNAME">QApplication</TT> that our button is              the main widget:</DD><PRECLASS="PROGRAMLISTING">app.setMainWidget(button)            </PRE><DT><A href="index.lxp@lxpwrap=x1067_252ehtm.htm#HELLO1SHOW"><IMGSRC="images/callouts/6.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(6)"></A></DT><DD>Then we <TTCLASS="FUNCTION">show()</TT>              the button to the world.</DD><DT><A href="index.lxp@lxpwrap=x1067_252ehtm.htm#HELLO1EXECLOOP"><IMGSRC="images/callouts/7.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(7)"></A></DT><DD>Until the application objects              starts the event loop, nothing will appear on screen.              The call <TTCLASS="FUNCTION">app.exec_loop()</TT> does              return a value, but we can safely disregard it.</DD></DL></DIV></DIV><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>Note that this is one of the few        instances where a method name differs between Python and C++:        the C++ method is called <TTCLASS="FUNCTION">exec()</TT>, which is        a reserved word in Python. Except for a few cases like this,        reading the C++ documentation for Python use demands little        more than a simple mental substitution.</P><P>Experienced Pythoneers will also note        that the parameters in PyQt function calls are positional        &#8212; not by keyword. In the old Tkinter GUI toolkit most        function calls take the form:</P><PRECLASS="PROGRAMLISTING">b = Button(root, text=label, command=func)        </PRE><P>where PyQt wants:</P><PRECLASS="PROGRAMLISTING">b = QPushButton(root, label, func)        </PRE><P>Just something to be aware of: keyword        parameters can be added in any old order, but positional        parameters have to be in the right position.</P></BLOCKQUOTE></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=c1036_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=x1149_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Qt Concepts</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c1036_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">A better Hello World</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

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