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_()) #運作程式