Previous | qpython - Features | Next |
qpython provides a command line that is generally similar in use to the standard Python interactive shell. Most of the time, it executes both single line and multi-line source code in a familiar way, but certain aspects of its behaviour (particularly where indentation is concerned) are quite different to that of standard interactive Python. For example, the following input would cause a SyntaxError in either Python or ipython:
>>> def fn(): ... print "Indented" ... >>> fn() ... Indented
Hopefully, these problems can be fixed over time, but they should not cause serious problems during casual use.
The command line supports tab-completion if the GNU readline support was built into your Python interpreter. Unfortunately, when it works, tab completion works rather too successfully: many Qt classes are wrapped in such a way that much of the Qt namespace is available through the instance:
>>> from qt import * >>> w = QWidget() >>> w. Display all 890 possibilities? (y or n)
Further work is required to determine an optimal solution to this problem. In the meantime, qpython provides the adir function to return a list of class attributes for an instance. This behaves like the built in dir function, but returns a substantially shorter list of objects. When looking for methods of Qt instances, adir is usually what you want to use:
>>> workspace = QWorkspace() >>> adir(workspace) ['setPaletteBackgroundColor', 'getWFlags', '__module__', 'paletteChange', 'imEndEvent', 'mouseReleaseEvent', 'clearWFlags', 'metric', 'keyPressEvent', 'dropEvent', 'windowList', 'showEvent', 'windowActivationChange', 'enterEvent', 'event', 'imStartEvent', 'mousePressEvent', 'dragMoveEvent', 'mouseDoubleClickEvent', 'setKeyCompression', 'eventFilter', 'create', 'imComposeEvent', 'scrollBarsEnabled', 'focusNextPrevChild', 'setMicroFocusHint', 'tile', 'destroy', 'keyReleaseEvent', 'childEvent', '__doc__', 'activeWindow', 'setPaletteBackgroundPixmap', 'focusOutEvent', 'contextMenuEvent', 'sender', 'closeEvent', 'hideEvent', 'tabletEvent', 'mouseMoveEvent', 'wheelEvent', 'enabledChange', 'setScrollBarsEnabled', 'dragEnterEvent', 'setWState', 'resetInputContext', 'getWState', 'timerEvent', 'resizeEvent', 'sizeHint', 'leaveEvent', 'clearWState', 'dragLeaveEvent', 'cascade', 'styleChange', 'setWFlags', 'moveEvent', 'updateMask', 'paintEvent', 'customEvent', 'focusInEvent', 'fontChange']
Although it is possible to input multi-line class and functions definitions at the Python prompt, most of the time this leads to frustration. The most productive forms of interactive use involve activites such as creating objects and invoking methods. However, it is sometimes necessary to write longer pieces of code, and it is not always desirable to create these externally.
qpython provides the editor function for this purpose; this displays a text editor in which code can be edited. When the code is ready to be used, it can be run as if it was input at the command line. To use the editor, you should invoke the editor function and assign the result to a variable. This prevents the editor widget from going out of scope, at which point it will be closed.
e = editor()
![]() |
Figure 1: The editor window with QScintilla-based syntax highlighting. |
---|
The code editor makes it possible for developers to prototype new classes and function that would otherwise be too tedious to write in this sort of environment. If PyQt was built with QScintilla support, the code in the editor window will be highlighted according to Python's syntax rules.
Previous | qpython - Features | Next |