每天都要有收获
利用高效开发工具Pycharm写PyQt,
- 安装pycharm
- 安装Anacanda python2.7版,我经常用2.7,利用虚拟环境安装3.6写pyqt5的代码
- cmd 下安装pyqt5、pyqt5-tools
- pycharm配置
安装Anacanda(略过)、虚拟环境配置
- 打开Anaconda Navigator
-
在Environments里,选择Create
-
填写名称和选择python版本,我选择3.6
- 解释器的路径就是 loacation所显示的路径 C:\Users\houyu\Anaconda2\envs\py36
安装pyqt5、pyqt5-tools
-
点击创建好的环境上的播放键,选择 open terminal
- 在控制台里输入:
pip install pyqt5
pip install pyqt5-tools
配置Pycharm环境
-
添加插件 qtdesigner
在pycharm-->file-->settings-->tools-->External Tools-->点击+号新建
-
填写名称,自己可以随便取名,我就起了Qtdesigner,文件地址,在pyqt5-tools文件下找,文件保存地址(点击旁边的insert选FileDir)
- 添加插件PyUic(用来转换UI-->.py文件)
program选择python.exe
arguements填写
-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
配置环境差不多完了,开始动手写一个
- 新建一个项目
- 点击右键选择 QtDesigner
-
拖拽控件TextEdit、pushButton、pushButton
- 保存
- 点击UI文件,右键PyUIC,转换成py文件
- 贴上两个文件的代码
看到有的文章说要删除段落,我这个没删除
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainWinPtt.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Serial(object):
def setupUi(self, Serial):
Serial.setObjectName("Serial")
Serial.resize(513, 368)
self.btn1 = QtWidgets.QPushButton(Serial)
self.btn1.setGeometry(QtCore.QRect(390, 40, 75, 23))
self.btn1.setObjectName("btn1")
self.btn2 = QtWidgets.QPushButton(Serial)
self.btn2.setGeometry(QtCore.QRect(390, 110, 75, 23))
self.btn2.setObjectName("btn2")
self.textEdit = QtWidgets.QTextEdit(Serial)
self.textEdit.setGeometry(QtCore.QRect(20, 10, 321, 331))
self.textEdit.setObjectName("textEdit")
self.retranslateUi(Serial)
self.btn1.clicked.connect(Serial.btn1_open)
self.btn2.clicked.connect(Serial.btn2_close)
QtCore.QMetaObject.connectSlotsByName(Serial)
def retranslateUi(self, Serial):
_translate = QtCore.QCoreApplication.translate
Serial.setWindowTitle(_translate("Serial", "Form"))
self.btn1.setText(_translate("Serial", "打开"))
self.btn2.setText(_translate("Serial", "关闭"))
# -*- coding: utf-8 -*-
# @Time : 2018/1/22 20:23
# @Author : houyujiang
# @Email : houyujiang@live.cn
# @File : pttTest.py
# @Software: PyCharm
import PyQt5
from mainWinPtt import Ui_Serial
from PyQt5 import QtWidgets
import sys
class MyWindows(QtWidgets.QMainWindow,Ui_Serial):
def __init__(self):
super(MyWindows, self).__init__()
self.setupUi(self)
def btn1_open(self):
self.textEdit.setText("hello.Open PTT~~~")
def btn2_close(self):
self.textEdit.setText("hello.Close PTT~~~")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindows()
window.show()
sys.exit(app.exec_())