1. Pycharm菜单添加PyQt工具
(1)【File/Setting/Tools/External Tools】
(2)添加QtDesigner(GUI界面设计工具,产生ui文件)
点击“+”打开对话框,按下图填写:Arguments和Working directory可以点击Inert Macto按钮选择
- Program:
/usr/bin/designer
- Arguments:
$FileName$
- Working directory:
$ProjectFileDir$
(3)添加pyuic5(
把Qt的ui文件转换成.py文件的工具 )
(3)添加pyuic5(
)
- Program:/usr/bin/pyuic5
- Arguments:$FileDirName$ -o $FileNameWithoutExtension$.py
- Working directory:$FileDir$
(4)添加pyrcc5(把图片等资源文件转成python能识别的文件)
- Program:/usr/bin/pyuic5
- Arguments:$FileDirName$ -o $FileNameWithoutExtension$.py
- Working directory:$FileDir$
2. 使用PyQt工具创建GUI程序
(1)打开pycharm,新建python项目,保存位置helloGui目录
(2)菜单【Tools/External Tools Qt/Qt Designer】打开designer
选New Form,建立新窗体。
保存界面为helloGui.ui文件,在pycharm中可看到helloGui.ui文件。
(2)菜单【Tools/External Tools Qt/PyUIC打开pyuic5,把ui文件转换成helloGui.py文件
helloGui.py文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'helloGui.ui'
#
# Created by: PyQt5 UI code generator 5.13.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(442, 284)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(140, 90, 151, 16))
self.label.setObjectName("label")
self.btn_Ok = QtWidgets.QPushButton(Dialog)
self.btn_Ok.setGeometry(QtCore.QRect(100, 230, 89, 30))
self.btn_Ok.setObjectName("btn_Ok")
self.btn_Cancel = QtWidgets.QPushButton(Dialog)
self.btn_Cancel.setGeometry(QtCore.QRect(220, 230, 89, 30))
self.btn_Cancel.setObjectName("btn_Cancel")
self.retranslateUi(Dialog)
self.btn_Ok.clicked.connect(Dialog.close)
self.btn_Cancel.clicked.connect(Dialog.close)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Hello Python GUI !"))
self.btn_Ok.setText(_translate("Dialog", "Ok"))
self.btn_Cancel.setText(_translate("Dialog", "Cancel"))
(3)新建helloMain.py文件,内容如下
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QDialog
import helloGui #引入helloGui.py中的界面类
if __name__ == '__main__':
app = QApplication(sys.argv)
myDlg = QDialog() #建立一个对话框对象
myUi = helloGui.Ui_Dialog() #建立界面类对象
myUi.setupUi(myDlg) #用界面类设置对话框对象
myDlg.show() #显示对话框
sys.exit(app.exec_()) #运行程序