pycharm配置Qt?Designer工具的圖文教程
之前一直使用QtCreator,在設(shè)計(jì)界面時(shí)非常方便,python早就集成了Qt模塊,在python中以pyQt的包存在,目前常用的是pyQt5,但是在pycharm中卻沒(méi)有找到像QtCreator那樣的編輯器,難道就只能通過(guò)代碼進(jìn)行界面編輯嗎,那速度真是太慢了。不要慌,界面肯定是有的,而且非常方便,首先打開(kāi)命令行安裝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
打開(kāi)File,選擇settings,然后找到External Tools,打開(kāi),第一次配置時(shí)界面如下
然后按照箭頭所示的操作進(jìn)行
如果使用的是Anaconda,可以參考我的目錄進(jìn)行配置,如果找不到designer.exe就在python的安裝目錄搜一下
然后是配置pyUIC,這個(gè)工具的作用是將UI文件轉(zhuǎn)換為.py文件
下面這個(gè)文件時(shí)打包用的,在命令行的用法是pyinstaller -F test.py
下面的工具是將ico圖標(biāo)放在qrc文件中,然后轉(zhuǎn)為py文件,用于生成打包后的圖標(biāo)
按照上面的步驟都安裝完成之后,可以在Tools中找到External Tools,然后里面就是我們剛剛創(chuàng)建的四個(gè)工具的快捷方式。
下面就是測(cè)試我們的設(shè)計(jì)工具,在項(xiàng)目文件中右鍵,會(huì)出來(lái)下面的菜單,在菜單中按照指示依次選擇
然后會(huì)打開(kāi)如下的界面,第一次打開(kāi)就是這個(gè)樣子,與qt Creator非常之像,簡(jiǎn)直就是一個(gè)模子刻出來(lái)的,用法也基本相同,但是沒(méi)有右鍵控件轉(zhuǎn)到槽函數(shù)的功能,哈哈哈。
我們選擇widget,然后創(chuàng)建一個(gè)新的UI文件,然后選擇保存,保存的位置就是你的pycharm工程目錄
簡(jiǎn)單的設(shè)計(jì)下面一個(gè)界面
然后關(guān)掉設(shè)計(jì)界面,回到pycharm,可以看到目錄中多了一個(gè)UI文件
在UI文件中我們右鍵,然后選擇External Tools
這時(shí)對(duì)應(yīng)UI的python代碼就已經(jīng)生成,可以看到目錄中多了一個(gè)與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", "打開(kāi)圖片")) self.detect.setText(_translate("Form", "邊緣檢測(cè)")) self.closewindow.setText(_translate("Form", "關(guān)閉窗口")) self.label.setText(_translate("Form", "show Image"))
然后通過(guò)自建main函數(shù)進(jìn)行調(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) # 打開(kāi)圖片按鈕槽函數(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_())
實(shí)驗(yàn)結(jié)果如下
到此這篇關(guān)于pycharm配置Qt Designer工具的圖文教程的文章就介紹到這了,更多相關(guān)pycharm Qt Designer內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python自動(dòng)定時(shí)任務(wù)schedule庫(kù)的使用方法
當(dāng)你需要在 Python 中定期執(zhí)行任務(wù)時(shí),schedule 庫(kù)是一個(gè)非常實(shí)用的工具,它可以幫助你自動(dòng)化定時(shí)任務(wù),本文給大家介紹了python自動(dòng)定時(shí)任務(wù)schedule庫(kù)的使用方法,需要的朋友可以參考下2024-02-02Python通過(guò)paramiko遠(yuǎn)程下載Linux服務(wù)器上的文件實(shí)例
今天小編就為大家分享一篇Python通過(guò)paramiko遠(yuǎn)程下載Linux服務(wù)器上的文件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12python3批量刪除豆瓣分組下的好友的實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇python3批量刪除豆瓣分組下的好友的實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06python3中for循環(huán)踩過(guò)的坑記錄
這篇文章主要給大家介紹了python3中for循環(huán)踩坑的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12python 插入日期數(shù)據(jù)到Oracle實(shí)例
這篇文章主要介紹了python 插入日期數(shù)據(jù)到Oracle實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03基于python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)代碼實(shí)例
這篇文章主要介紹了基于python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09