pycharm配置Qt?Designer工具的圖文教程
之前一直使用QtCreator,在設(shè)計界面時非常方便,python早就集成了Qt模塊,在python中以pyQt的包存在,目前常用的是pyQt5,但是在pycharm中卻沒有找到像QtCreator那樣的編輯器,難道就只能通過代碼進行界面編輯嗎,那速度真是太慢了。不要慌,界面肯定是有的,而且非常方便,首先打開命令行安裝pyqt包,命令如下
pip install?PyQt5 -i https://pypi.doubanio.com/simple pip install PyQt5-tools -i https://pypi.doubanio.com/simple pip install paramiko -i https://pypi.doubanio.com/simple pip install pyinstaller -i https://pypi.doubanio.com/simple
安裝完了之后,下面就是在pycharm中配置qt designer
打開File,選擇settings,然后找到External Tools,打開,第一次配置時界面如下

然后按照箭頭所示的操作進行

如果使用的是Anaconda,可以參考我的目錄進行配置,如果找不到designer.exe就在python的安裝目錄搜一下

然后是配置pyUIC,這個工具的作用是將UI文件轉(zhuǎn)換為.py文件

下面這個文件時打包用的,在命令行的用法是pyinstaller -F test.py

下面的工具是將ico圖標放在qrc文件中,然后轉(zhuǎn)為py文件,用于生成打包后的圖標

按照上面的步驟都安裝完成之后,可以在Tools中找到External Tools,然后里面就是我們剛剛創(chuàng)建的四個工具的快捷方式。

下面就是測試我們的設(shè)計工具,在項目文件中右鍵,會出來下面的菜單,在菜單中按照指示依次選擇

然后會打開如下的界面,第一次打開就是這個樣子,與qt Creator非常之像,簡直就是一個模子刻出來的,用法也基本相同,但是沒有右鍵控件轉(zhuǎn)到槽函數(shù)的功能,哈哈哈。

我們選擇widget,然后創(chuàng)建一個新的UI文件,然后選擇保存,保存的位置就是你的pycharm工程目錄
簡單的設(shè)計下面一個界面

然后關(guān)掉設(shè)計界面,回到pycharm,可以看到目錄中多了一個UI文件

在UI文件中我們右鍵,然后選擇External Tools

這時對應(yīng)UI的python代碼就已經(jīng)生成,可以看到目錄中多了一個與UI文件同名的py文件

內(nèi)容如下
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'demo.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1066, 796)
self.horizontalLayoutWidget = QtWidgets.QWidget(Form)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 1051, 80))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.openimg = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.openimg.setObjectName("openimg")
self.horizontalLayout.addWidget(self.openimg)
self.detect = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.detect.setObjectName("detect")
self.horizontalLayout.addWidget(self.detect)
self.closewindow = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.closewindow.setObjectName("closewindow")
self.horizontalLayout.addWidget(self.closewindow)
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(Form)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(10, 90, 1051, 701))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.retranslateUi(Form)
self.openimg.clicked.connect(Form.shouImg)
self.detect.clicked.connect(Form.edgeDetect)
self.closewindow.clicked.connect(Form.close)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.openimg.setText(_translate("Form", "打開圖片"))
self.detect.setText(_translate("Form", "邊緣檢測"))
self.closewindow.setText(_translate("Form", "關(guān)閉窗口"))
self.label.setText(_translate("Form", "show Image"))然后通過自建main函數(shù)進行調(diào)用,代碼如下
from demo import Ui_Form
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QMainWindow, QCheckBox
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtCore import *
from PyQt5.QtCore import pyqtSlot
import sys
import os
import cv2
import numpy as np
from PyQt5.QtGui import *
class MainWindow(QMainWindow, Ui_Form):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
# 打開圖片按鈕槽函數(shù)
def shouImg(self):
filePath = self.getFileDir()
img = cv2.imread(filePath[0][0])
img_dis = QImage(img, img.shape[1], img.shape[0], QImage.Format_RGB888)
# 加載圖片,并設(shè)定圖片大小
img_dis = QPixmap(img_dis).scaled(int(img.shape[1]), int(img.shape[0]))
width = img_dis.width() ##獲取圖片寬度
height = img_dis.height() ##獲取圖片高度
if width / self.label.width() >= height / self.label.height(): ##比較圖片寬度與label寬度之比和圖片高度與label高度之比
ratio = width / self.label.width()
else:
ratio = height / self.label.height()
new_width = int(width / ratio) ##定義新圖片的寬和高
new_height = int(height / ratio)
new_img = img_dis.scaled(new_width, new_height) ##調(diào)整圖片尺寸
# img_dis = QPixmap(img_dis).scaled(int(img.shape[1]), int(img.shape[0]))
self.label.setPixmap(new_img)
# 獲取文件地址函數(shù)
def getFileDir(self):
try:
self.file_path = QFileDialog.getOpenFileNames(self, "select file", "./", "*.*")
except Exception as e:
print(e)
return self.file_path
def edgeDetect(self):
# 自己發(fā)揮
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())實驗結(jié)果如下

到此這篇關(guān)于pycharm配置Qt Designer工具的圖文教程的文章就介紹到這了,更多相關(guān)pycharm Qt Designer內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python自動定時任務(wù)schedule庫的使用方法
當你需要在 Python 中定期執(zhí)行任務(wù)時,schedule 庫是一個非常實用的工具,它可以幫助你自動化定時任務(wù),本文給大家介紹了python自動定時任務(wù)schedule庫的使用方法,需要的朋友可以參考下2024-02-02
Python通過paramiko遠程下載Linux服務(wù)器上的文件實例
今天小編就為大家分享一篇Python通過paramiko遠程下載Linux服務(wù)器上的文件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python3批量刪除豆瓣分組下的好友的實現(xiàn)代碼
下面小編就為大家?guī)硪黄猵ython3批量刪除豆瓣分組下的好友的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
python 插入日期數(shù)據(jù)到Oracle實例
這篇文章主要介紹了python 插入日期數(shù)據(jù)到Oracle實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
基于python實現(xiàn)圖片轉(zhuǎn)字符畫代碼實例
這篇文章主要介紹了基于python實現(xiàn)圖片轉(zhuǎn)字符畫代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09

