-

PyQt安装程序附带一个名为Qt Designer的GUI创建器工具使用其简单的拖放界面,可以快速创建GUI界面,而无需编写代码。但是,不是像Visual Studio这样的IDE。因此,Qt Designer没有调试和创建应用程序的功能。

使用Qt Designer创建GUI界面首先为应用程序选择顶级窗口。

Qt设计师1

然后,您可以从左窗格中的窗口小部件框中拖放所需的窗口小部件。您还可以为表单上放置的小部件的属性分配值。

Qt Designer2

The designed form is saved as demo.ui. This ui file contains XML representation of widgets and their properties in the design. This design is translated into Python equivalent by using pyuic4 command line utility. This utility is a wrapper for uic module. The usage of pyuic4 is as follows −

pyuic4 –x demo.ui –o demo.py

In the above command, -x switch adds a small amount of additional code to the generated XML so that it becomes a self-executable standalone application.

if __name__ == "__main__":
   import sys
   app = QtGui.QApplication(sys.argv)
   Dialog = QtGui.QDialog()
   ui = Ui_Dialog()
   ui.setupUi(Dialog)
   Dialog.show()
   sys.exit(app.exec_())

The resultant python script is executed to show the following dialog box −

对话框

The user can input data in input fields but clicking on Add button will not generate any action as it is not associated with any function. Reacting to user-generated response is called as event handling.