index.lxp@lxpwrap=x2976_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 1,070 行 · 第 1/2 页
HTM
1,070 行
<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>Basic widgets</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="Widget foundations: QWidget"HREF="x2765.htm"><LINKREL="NEXT"TITLE="Advanced widgets"HREF="x3270.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=x2765_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=x3270_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Basic widgets</A></H1><P>All basic screen components are available in PyQt: buttons, frames, edit controls, listboxes and comboboxes. All these widgets can be drawn in any number of styles, and you can even define your own style. Note that the window titlebar and borders are not defined by the widget style, but by the system you are running. The borders in these screenshots are from the KDE System++ style. </P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Basic widgets in the CDE style</P></DIV></P></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Basic widgets in the motif style</P></DIV></P></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Basic widgets in the motif plus style</P></DIV></P></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Basic widgets in the platinum style</P></DIV></P></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Basic widgets in the SGI style</P></DIV></P></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Basic widgets in the Windows style</P></DIV></P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QFrame</A></H2><P>Frames are used to group other widgets — either visibly (for instance by drawing a nice bevel around them), or invisibly (by managing the geometry of those widgets. PyQt offers all the usual options, from panels to ridges to bevels, with horizontal and vertical lines thrown in for good measure. </P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QPushButton</A></H2><P> Pushbuttons are the mainstay of gui programming. They can be adorned with text or with a picture, but not both (you need a <TTCLASS="CLASSNAME">QToolButton</TT> for that). <TTCLASS="CLASSNAME">QPushButton</TT>s are based on an abstraction of all button functionality, namely <TTCLASS="CLASSNAME">QButton</TT>, which is also the parent of <TTCLASS="CLASSNAME">QCheckBox</TT>, <TTCLASS="CLASSNAME">QRadioButton</TT> and <TTCLASS="CLASSNAME">QToolButton</TT>. In honor of QPushButton's central importance, I want to present a ‘Hello' application with four buttons, each in a different style. This also shows a frame. </P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-7. buttons.py - Four pushbuttons saying ‘hello'.</B></P><PRECLASS="PROGRAMLISTING">## buttons.py#from qt import *import sysclass MainWindow(QMainWindow): def __init__(self, *args): apply(QMainWindow.__init__, (self,) + args) self.setCaption("Buttons") self.grid=QGrid(2, self) self.grid.setFrameShape(QFrame.StyledPanel) self.bn1=QPushButton("Hello World", self.grid) self.bn1.setFlat(1) self.bn2=QPushButton("Hello World", self.grid) self.bn2.setDefault(1) self.bn3=QPushButton("Hello World", self.grid) self.bn3.setToggleButton(1) self.bn3.setDown(1) self.bn4=QPushButton("Hello", self.grid) self.setCentralWidget(self.grid) 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>buttons.py</P></DIV></P></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QLabel</A></H2><P>Labels are ubiquitous in a gui application — and the PyQt <TTCLASS="CLASSNAME">QLabel</TT> offers much more than just plain-text labels for use in dialog boxes. PyQt labels can also contain rich text or a <TTCLASS="CLASSNAME">QMovie</TT>, such as an animated GIF or PNG. Through the <TTCLASS="FUNCTION">setBuddy</TT> method, a <TTCLASS="CLASSNAME">QLabel</TT> can be associated with another control. If any character in the label text is prefixed by an ampersand — & — that character will be shown underlined, and by pressing alt-character, the user can jump to the control associated with the label through the buddy property. </P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-8. label.py - a label associated with an edit control</B></P><PRECLASS="PROGRAMLISTING">## label.py#import sysfrom qt import *class dlgLabel(QDialog): def __init__(self,parent = None,name = None,modal = 0,fl = 0): QDialog.__init__(self,parent,name,modal,fl) self.setCaption("label dialog") if name == None: self.setName("dlgLabel") self.layout=QHBoxLayout(self) self.layout.setSpacing(6) self.layout.setMargin(11) self.label=QLabel("&Enter some text", self) self.edit=QLineEdit(self) self.label.setBuddy(self.edit) self.layout.addWidget(self.label) self.layout.addWidget(self.edit)if __name__ == '__main__': app = QApplication(sys.argv) QObject.connect(app, SIGNAL('lastWindowClosed()') , app , SLOT('quit()') ) win = dlgLabel() app.setMainWidget(win) win.show() app.exec_loop() </PRE></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>label.py</P></DIV></P></DIV><P>If you press alt-e after starting the <TTCLASS="FILENAME">label.py</TT> script (which is on the CD-ROM), you'll see the cursor appearing in the edit field.</P><P>You might wonder why the cursor is not the control that accepts user input when you start the script — this is a property of PyQt. On starting an application, the main window has the focus, not the controls associated with it. If you want to make the user's life easier, call <TTCLASS="FUNCTION">setFocus()</TT> on the main widget in the <TTCLASS="FUNCTION">__init__</TT> of the main window:</P><PRECLASS="PROGRAMLISTING">## label2.py#import sysfrom qt import *class dlgLabel(QDialog): def __init__(self,parent = None,name = None,modal = 0,fl = 0): QDialog.__init__(self,parent,name,modal,fl) self.setCaption("label dialog") if name == None: self.setName("dlgLabel") self.layout=QHBoxLayout(self) self.layout.setSpacing(6) self.layout.setMargin(11) self.label=QLabel("&Enter some text", self) self.edit=QLineEdit(self) self.label.setBuddy(self.edit) self.layout.addWidget(self.label) self.layout.addWidget(self.edit) self.edit.setFocus()if __name__ == '__main__': app = QApplication(sys.argv) QObject.connect(app, SIGNAL('lastWindowClosed()'), app, SLOT('quit()')) win = dlgLabel() app.setMainWidget(win) win.show() app.exec_loop() </PRE><P>A label is a QWidget; it is thus quite possible to handle key and mouse events in a label. You might, for instance, want to make a clickable label that looks like an URL.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QRadioButton</A></H2><P>Radio buttons always remind of my tax return forms - check <SPAN><ICLASS="EMPHASIS">one</I></SPAN> and only <SPAN><ICLASS="EMPHASIS">one</I></SPAN> out of a certain number of choices. Radio buttons should not be used if there are more than five choices at most, and you would be well advised to limit yourself to no more than three. A constellation of radio buttons has the advantage that all options are visible at the same time, but it takes a lot of screen space. Do not use checkboxes instead of radio buttons for exclusive choices. People will get confused. </P><P>Radio buttons include their labels — and these labels can again be marked with an ampersand (&) for easy selection. In order to force the ‘one and only one' choice, combine the mutually exclusive radio buttons in a <TTCLASS="CLASSNAME">QButtonGroup</TT>, or one of the descendants: <TTCLASS="CLASSNAME">QVButtonGroup</TT> for vertical layouts (recommended) or <TTCLASS="CLASSNAME">QHButtonGroup</TT> for horizontal layouts (these look rather weird). A radiobutton can be initialized with <TTCLASS="FUNCTION">setChecked()</TT>.</P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-9. radio.py - a group of mutually exclusive options</B></P><PRECLASS="PROGRAMLISTING">## label.py#import sysfrom qt import *class dlgRadio(QDialog): def __init__(self,parent = None,name = None,modal = 0,fl = 0): QDialog.__init__(self,parent,name,modal,fl) self.setCaption("radiobutton dialog") if name == None: self.setName("dlgRadio") self.layout=QVBoxLayout(self)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?