每天都要有收獲
利用高效開發(fā)工具Pycharm寫PyQt,
- 安裝pycharm
- 安裝Anacanda python2.7版,我經(jīng)常用2.7凸克,利用虛擬環(huán)境安裝3.6寫pyqt5的代碼
- cmd 下安裝pyqt5、pyqt5-tools
- pycharm配置
安裝Anacanda(略過)吭敢、虛擬環(huán)境配置
- 打開Anaconda Navigator
-
在Environments里汛蝙,選擇Create
-
填寫名稱和選擇python版本,我選擇3.6
- 解釋器的路徑就是 loacation所顯示的路徑 C:\Users\houyu\Anaconda2\envs\py36
安裝pyqt5豺撑、pyqt5-tools
-
點(diǎn)擊創(chuàng)建好的環(huán)境上的播放鍵质帅,選擇 open terminal
- 在控制臺里輸入:
pip install pyqt5
pip install pyqt5-tools
配置Pycharm環(huán)境
-
添加插件 qtdesigner
在pycharm-->file-->settings-->tools-->External Tools-->點(diǎn)擊+號新建
-
填寫名稱适揉,自己可以隨便取名,我就起了Qtdesigner,文件地址煤惩,在pyqt5-tools文件下找嫉嘀,文件保存地址(點(diǎn)擊旁邊的insert選FileDir)
- 添加插件PyUic(用來轉(zhuǎn)換UI-->.py文件)
program選擇python.exe
arguements填寫
-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
配置環(huán)境差不多完了,開始動手寫一個(gè)
- 新建一個(gè)項(xiàng)目
- 點(diǎn)擊右鍵選擇 QtDesigner
-
拖拽控件TextEdit魄揉、pushButton剪侮、pushButton
- 保存
- 點(diǎn)擊UI文件,右鍵PyUIC,轉(zhuǎn)換成py文件
- 貼上兩個(gè)文件的代碼
看到有的文章說要刪除段落洛退,我這個(gè)沒刪除
# -*- 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", "關(guān)閉"))
# -*- 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_())