更新日志:
2017年8月14日
如果需要相关安装的指南,可以查看Mac下Pycharm集成PyQt5并使用
注意:该文档是简单的功能实现,事实上,在工程上,我们常常要做到界面和逻辑分离,请参见 PyQt5 如何让界面和逻辑分离简介。
注意:pyqt4 与 pyqt5 还是有一些不同的,要注意资料的不同版本号。
方法1:直接写 .py
这种方法仅用于熟悉、了解大概的 ui化过程,事实上,我们很少自己写 ui 的 py 文件,一般采用后面介绍的方法2.
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QWidget
app = QApplication(sys.argv)
window = QWidget()
window.resize(550, 150)
window.show()
app.exec_()
方法2:使用 QtDesigner 将 .ui 转换为 .py
使用 QtDesigner 直接绘制 ui 图,然后再使用转换器将 ui 图直接生成 py 文件。这种方法更推荐,也是更习惯的方法。
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget
import sys
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.comboBox = QtWidgets.QComboBox(Dialog)
self.comboBox.setGeometry(QtCore.QRect(60, 40, 104, 26))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox_2 = QtWidgets.QComboBox(Dialog)
self.comboBox_2.setGeometry(QtCore.QRect(190, 40, 104, 26))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(70, 10, 121, 16))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.comboBox.setItemText(0, _translate("Dialog", "Toefl"))
self.comboBox.setItemText(1, _translate("Dialog", "Ielts"))
self.comboBox_2.setItemText(0, _translate("Dialog", "RS_Normal"))
self.comboBox_2.setItemText(1, _translate("Dialog", "R_AR"))
self.label.setText(_translate("Dialog", "background tool"))
如上内容为自动生成的。然后在 main.py 中调用,代码如下。
app = QtWidgets.QApplication(sys.argv)
gui = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(gui)
gui.show()
app.exec_()
或者使用更标准的写法,上面程序可以写为:
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
gui = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(gui)
gui.show()
sys.exit(app.exec_())
PyQt 中不同组件的介绍
Widget: A rectangular region on the screen for display and user interaction. They include buttons, sliders, views, dialogs, windows, etc. All widgets will display something on the screen, and many will also accept user input from the keyboard or mouse. The word "widget" comes from Unix, on Windows they are called "controls".
Window: A "top level" widget. A window is the top of a parent/child hiearchy, and are usually displayed with a titlebar and border. The underlying windowing system (Windows, KDE, GNOME, etc) will provide policies for the windows, such as titlebar/border style, placement, focus, etc.
Dialog: A special kind of window, usually temporary. They may or may not have a different titlebar appearance. They are presented to the user for the purpose of notification or gathering input, and typically have OK, Cancel, etc., buttons on the bottom or right.
简单的页面交互
理论知识及简单示例:PyQt - Signals & Slots
具体的完整示例:Pop up dialog from one button on the main window, PyQt5