index.lxp@lxpwrap=x966_252ehtm.htm

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

HTM
310
字号
    <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>Stepping along</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="Debugging"HREF="c883.htm"><LINKREL="PREVIOUS"TITLE="Setting breakpoints"HREF="x945.htm"><LINKREL="NEXT"TITLE="Debugging Techniques"HREF="x993.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=x945_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 5. Debugging</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x993_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Stepping along</A></H1><P>If your application goes wrong in the      initialization phase, then you might want to single step every      statement until you arrive at the offending statement. Let's      debug the same script as in the previous section, but altered      slightly to introduce a pesky typo. If you can spot it from      eyeballing the code alone, then you've probably skipped ahead to      <A href="index.lxp@lxpwrap=c1267_252ehtm.htm">Chapter 7</A>, on signals and slots.</P><PRECLASS="PROGRAMLISTING">&#13;## button2.py#from qt import *import sysclass MainWindow(QMainWindow):    def __init__(self, *args):        apply(QMainWindow.__init__, (self,) + args)        self.setCaption("Button")        self.grid=QGrid(2, self)        self.grid.setFrameShape(QFrame.StyledPanel)        self.bn=QPushButton("Hello World", self.grid)        self.bn.setDefault(1)        self.connect(self.bn, SIGNAL("clicked()"),                     self.slotSlot())        self.setCentralWidget(self.grid)    def slotSlot(self):        i = 1/0def main(args):    app=QApplication(args)    win=MainWindow()    win.show()    app.connect(app, SIGNAL("lastWindowClosed()"),                app, SLOT("quit()"))    app.exec_loop()if __name__=="__main__":    main(sys.argv)    </PRE><P>Running this code won't place the window on screen.      Instead, it ends with the following stacktrace:</P><PRECLASS="SCREEN">Traceback (most recent call last):  File "&#60;stdin&#62;", line 37, in ?  File "&#60;stdin&#62;", line 30, in main  File "&#60;stdin&#62;", line 21, in __init__  File "&#60;stdin&#62;", line 26, in slotSlotZeroDivisionError: integer division or modulo by zero    </PRE><P>If you single step this using      <SPANCLASS="APPLICATION">BlackAdder</SPAN> you'll notice that      directly after the line:</P><PRECLASS="PROGRAMLISTING">        self.connect(self.bn, SIGNAL("clicked()"),                     self.slotSlot())    </PRE><P>Python continues with:</P><PRECLASS="PROGRAMLISTING">    def slotSlot(self):    </PRE><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Accidentally stepping into a function.</P></DIV></P></DIV><P>Armed with this knowledge, it's easy to      see what went wrong: we <SPAN><ICLASS="EMPHASIS">called</I></SPAN> the function      in the <TTCLASS="FUNCTION">connect</TT> line by adding the two      brackets, passing the result of the      <TTCLASS="FUNCTION">slotSlot()</TT> function to the      <TTCLASS="FUNCTION">connect()</TT>, instead of giving the      <SPAN><ICLASS="EMPHASIS">function</I></SPAN> object as a parameter to      <TTCLASS="FUNCTION">connect</TT>. Simply changing the line      to:</P><PRECLASS="PROGRAMLISTING">        self.connect(self.bn, SIGNAL("clicked()"),                     self.slotSlot)    </PRE><P>And the bug is fixed! Incidentally, this      also shows that you can create new signal/slot connections on      the fly, from the output of a function&#8212;this is something to      be aware of when creating a very dynamic application.</P></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=x945_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=x993_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Setting breakpoints</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c883_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Debugging Techniques</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

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