index.lxp@lxpwrap=x3581_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 755 行 · 第 1/2 页
HTM
755 行
<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™</div> </div> </td></tr></table> <div align="center" class="author"> <a href="../products.lxp">Products</a> | <a href="../wheretobuy.lxp">Where to buy</a> | <a href="../bookstore.lxp">Retailers</a> | <a href="../faq.lxp">FAQ</a> | <a href="../writeforus.lxp">Write for Us.</a> | <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>Dialogs and Standard Dialogs</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 Class Hierarchy"HREF="c2591.htm"><LINKREL="PREVIOUS"TITLE="Layout managers"HREF="x3410.htm"><LINKREL="NEXT"TITLE="Qt Utility classes and their Python equivalents"HREF="x3738.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=x3410_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 10. Qt Class Hierarchy</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x3738_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Dialogs and Standard Dialogs</A></H1><P>Bill Gates is apocryphically reported to have once shouted ‘Why must everyone in my company write his own file-open code? Go and build something that works for <SPAN><ICLASS="EMPHASIS">every</I></SPAN> application!". And thus the Windows standard file open dialog was born. This dialog has gone through several versions, necessitating continuous rewriting. All the same, the idea was a good idea, and Qt implements several standard dialogs that look and feel just like the Windows common dialogs, but are a lot easier to program. We'll implement common dialog PyQt lacks, for searching and replacing, in <A href="index.lxp@lxpwrap=x6734_252ehtm.htm">the Section called <I>Non-modal: Search and replace</I> in Chapter 19</A>, Using Dialog Windows.</P><DIVCLASS="SECT2"><H2CLASS="SECT2">QDialog</A></H2><P><TTCLASS="CLASSNAME">QDialog</TT> is the parent of all dialog classes. A dialog window is a window that pops up over the application window. These can be modal (where it will block the rest of the application) or modeless (where the user can continue working in the main screen of the application). Dialogs are commonly closed with OK or Cancel buttons. There is no reason to make a dialog a fixed size; you can give it a <TTCLASS="CLASSNAME">QSizeGrip</TT>, and if you use <TTCLASS="CLASSNAME">QLayout</TT> layout management, the contents will be resized quite nicely. A modal dialog has its own <TTCLASS="FUNCTION">exec_loop</TT>; a modeless dialog can be constructed, shown, and hidden, but is part of its parents event loop. </P><P>Of course, there are many other occasions where you will want to create custom dialog boxes. PyQt provides for plain dialog boxes, expanding dialog boxes, tabbed dialog boxes and wizards.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QMessageBox</A></H2><P>A QMessageBox is a very simple standard dialog class. Message boxes are always modal, and can be used to inform, warn or frighten the user. Message texts should preferably short, specific, and as non-threatening as possible.</P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-14. dialogs.py - opening message and default dialogs boxes </B></P><PRECLASS="PROGRAMLISTING">## dialogs.py#import sysfrom qt import *class MainWindow(QMainWindow): def __init__(self, *args): apply(QMainWindow.__init__, (self, ) + args) self.setCaption("Network Client") self.actionInformation=QAction(self, "Information") self.actionInformation.setText("Informational Message") self.actionInformation.setMenuText("&Information") self.actionInformation.setStatusTip("Show an informational mesagebox.") self.connect(self.actionInformation, SIGNAL("activated()"), self.slotInformation) self.actionWarning=QAction(self, "Warning") self.actionWarning.setText("Warning Message") self.actionWarning.setMenuText("&Warning") self.actionWarning.setStatusTip("Show a warning mesagebox.") self.connect(self.actionWarning, SIGNAL("activated()"), self.slotWarning) self.actionCritical=QAction(self, "Critical") self.actionCritical.setText("Critical Message") self.actionCritical.setMenuText("&Critical") self.actionCritical.setStatusTip("Show an informational mesagebox.") self.connect(self.actionCritical, SIGNAL("activated()"), self.slotCritical) self.actionAbout=QAction(self, "About") self.actionAbout.setText("About") self.actionAbout.setMenuText("&About") self.actionAbout.setStatusTip("Show an about box.") self.connect(self.actionAbout, SIGNAL("activated()"), self.slotAbout) self.actionAboutQt=QAction(self, "AboutQt") self.actionAboutQt.setText("About Qt Message") self.actionAboutQt.setMenuText("About &Qt") self.actionAboutQt.setStatusTip("Show an about box for Qt.") self.connect(self.actionAboutQt, SIGNAL("activated()"), self.slotAboutQt) self.actionFile=QAction(self, "OpenFile") self.actionFile.setText("Open File") self.actionFile.setMenuText("&Open") self.actionFile.setStatusTip("Open a file.") self.connect(self.actionFile, SIGNAL("activated()"), self.slotFile) self.actionFont=QAction(self, "Font") self.actionFont.setText("Select a font") self.actionFont.setMenuText("&Font") self.actionFont.setStatusTip("Select a font") self.connect(self.actionFont, SIGNAL("activated()"), self.slotFont) self.actionColor=QAction(self, "Color") self.actionColor.setText("Select a color") self.actionColor.setMenuText("&Color") self.actionColor.setStatusTip("Select a color") self.connect(self.actionColor, SIGNAL("activated()"), self.slotColor) # Statusbar self.statusBar=QStatusBar(self) # Define menu self.messageMenu=QPopupMenu() self.actionInformation.addTo(self.messageMenu) self.actionWarning.addTo(self.messageMenu) self.actionCritical.addTo(self.messageMenu) self.dialogMenu=QPopupMenu() self.actionFile.addTo(self.dialogMenu) self.actionFont.addTo(self.dialogMenu) self.actionColor.addTo(self.dialogMenu) self.helpMenu=QPopupMenu() self.actionAbout.addTo(self.helpMenu) self.actionAboutQt.addTo(self.helpMenu) self.menuBar().insertItem("&Messages", self.messageMenu) self.menuBar().insertItem("&Standard dialogs", self.dialogMenu) self.menuBar().insertItem("&Help", self.helpMenu) def slotInformation(self): QMessageBox.information(self, "Information", "A plain, informational message") def slotWarning(self): QMessageBox.warning(self, "Warning", "What you are about to do will do some serious harm .") def slotCritical(self): QMessageBox.critical(self, "Critical", "A critical error has occurred.\nProcessing will be stopped!") def slotAbout(self): QMessageBox.about(self, "About me", "A demo of message boxes and standard dialogs.") def slotAboutQt(self): QMessageBox.aboutQt(self) def slotFile(self): filename=QFileDialog.getOpenFileName("", "*.py", self, "FileDialog") def slotFont(self): (font, ok) = QFontDialog.getFont(self, "FontDialog") def slotColor(self): color=QColorDialog.getColor(QColor("linen"), self, "ColorDialog")def 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></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Giving the user some information.</P></DIV></P></DIV><DIV
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?