这是一个命令行界面pyuic4 UIC模块。该命令具有以下语法:
pyuic4 [options] .ui-file
Pyuic4 (选项) .ui -o file
The full set of command line options is:
-h, --help
A help message is written to stdout.
--version
The version number is written to stdout.
-i
The Python code is generated using an indentation of
-o
The Python code generated is written to the file
-p, --preview
The GUI is created dynamically and displayed. No Python code is generated.
-w, --pyqt3-wrapper
The generated Python code includes a small wrapper that allows the GUI to be used in the same way as it is used in PyQt v3.
-x, --execute
The generated Python code includes a small amount of additional code that creates and displays the GUI when it is executes as a standalone application.
--from-imports
Resource modules are imported using from . import rather than a simple import.
--resource-suffix
The suffix
The code that is generated has an identical structure to that generated by Qt’s uic and can be used in the same way.
The code is structured as a single class that is derived from the Python object type. The name of the class is the name of the toplevel object set in Designer with Ui_ prepended. (In the C++ version the class is defined in the Ui namespace.) We refer to this class as the form class.
The class contains a method called setupUi(). This takes a single argument which is the widget in which the user interface is created. The type of this argument (typically QDialog, QWidget or QMainWindow) is set in Designer. We refer to this type as the Qt base class.
In the following examples we assume that a .ui file has been created containing a dialog and the name of the QDialog object is ImageDialog. We also assume that the name of the file containing the generated Python code is ui_imagedialog.py. The generated code can then be used in a number of ways.
The first example shows the direct approach where we simply create a simple application to create the dialog:
import sys
from PyQt4.QtGui import QApplication, QDialog
from ui_imagedialog import Ui_Image
Dialogapp = QApplication(sys.argv)
window = QDialog()
ui = Ui_ImageDialog()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
The second example shows the single inheritance approach where we sub-class QDialog and set up the user interface in the __init__() method:
from PyQt4.QtGui import QDialog
from ui_imagedialog import Ui_ImageDialog
class ImageDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
# Set up the user interface from Designer.
self.ui = Ui_ImageDialog()
self.ui.setupUi(self)
# Make some local modifications.
self.ui.colorDepthCombo.addItem("2 colors (1 bit per pixel)")
# Connect up the buttons.
self.ui.okButton.clicked.connect(self.accept)
self.ui.cancelButton.clicked.connect(self.reject)
The third example shows the multiple inheritance approach:
from PyQt4.QtGui import QDialog
from ui_imagedialog import Ui_ImageDialog
class ImageDialog(QDialog, Ui_ImageDialog):
def __init__(self):
QDialog.__init__(self)
# Set up the user interface from Designer.
self.setupUi(self)
# Make some local modifications.
self.colorDepthCombo.addItem("2 colors (1 bit per pixel)") # Connect up the buttons. self.okButton.clicked.connect(self.accept) self.cancelButton.clicked.connect(self.reject)
It is also possible to use the same approach used in PyQt v3. This is shown in the final example:
from ui_imagedialog import ImageDialogclass MyImageDialog(ImageDialog): def __init__(self): ImageDialog.__init__(self) # Make some local modifications. self.colorDepthCombo.addItem("2 colors (1 bit per pixel)") # Connect up the buttons. self.okButton.clicked.connect(self.accept) self.cancelButton.clicked.connect(self.reject)
For a full description see the Qt Designer Manual in the Qt Documentation.