win10系统PyCharm2020.3.1 pyqt5 python3.8.5开发环境配置

一、pyqt5 pyqt5-tools 安装

新建项目

 

菜单 File/Settings

设置一下国内镜像源

https://pypi.tuna.tsinghua.edu.cn/simple/

https://pypi.python.org/simple    默认的

这个是国外的源, 我们国内通过这个源安装包比较慢, 甚至无法安装,
所以我们需要将源改为我们国内的源
国内知名的几个源

豆瓣(douban) https://pypi.douban.com/simple
阿里云 https://mirrors.aliyun.com/pypi/simple
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple

 

选择pyqt5,这里用的默认选择的版本。pyqt5-tools-5.15.2.3也是默认选择版本,点击等待完成安装。

二、安装完成之后配置pyqt5designer、pyuic5、pyrcc5

安装完成之后,相应工具安装在此目录:

c:\python\python38\scripts\  (安装的Python的scripts子目录中)qt5-tools.exe

C:\Users\Administrator\PycharmProjects\pythonProject\venv\Scripts\

pyqt5-tools.exe

qt5-tools.exe

pyuic5.exe

pyrcc5.exe

。。。。等工具

 

开始配置:菜单 File/Settings(当然也可使用命令行,后述……)

配置QtDesigner
Name:QtDesigner
Program:C:\Program Files\Python38\Scripts\pyqt5designer.exe    c:\python\python38\scripts\pyqt5-tools.exe    
                                                                                                          C:\Users\Administrator\PycharmProjects\pythonProject\venv\pyqt5-tools.exe
#这里输入你的python安装路径下的Scripts\pyqt5designer.exe就好
Arguments:designer
Working directory:$ProjectFileDir$
点击OK

配置PyUic5
Name:PyUic5
Program:C:\Program Files\Python38\Scripts\pyuic5.exe        C:\Users\Administrator\PycharmProjects\pythonProject\venv\Scripts\pyuic5.exe
#这里输入你的python安装路径下的Scripts\pyuic5.exe就好
Arguments:$FileName$ -o $FileNameWithoutExtension$.py
Working directory:$FileDir$
点击OK

配置PyRcc5
Name:pyrcc5
Program:C:\Program Files\Python38\Scripts\pyrcc5.exe        C:\Users\Administrator\PycharmProjects\pythonProject\venv\Scripts\pyrcc5.exe
#这里输入你的python安装路径下的Scripts\pyrcc5.exe就好
Arguments:$FileName$ -o $FileNameWithoutExtension$.py
Working directory:$FileDir$
 

配置好3个,开始使用了。

右击

或者: Tools /External Tools...

三、命令行的使用

(venv) C:\Users\Administrator\PycharmProjects\pythonProject>pyqt5-tools
Usage: pyqt5-tools [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  designer
  installuic
  qmlscene
  qmltestrunner

(venv) C:\Users\Administrator\PycharmProjects\pythonProject>

(venv) C:\Users\Administrator\PycharmProjects\pythonProject>qt5-tools
Usage: qt5-tools [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  assistant
  canbusutil
  designer
  lconvert
  licheck64
  linguist
  lprodump
  lrelease
  lrelease-pro
  lupdate
  lupdate-pro
  moc
  pixeltool
  qcollectiongenerator
  qdbus
  qdbuscpp2xml
  qdbusviewer
  qdbusxml2cpp
  qdistancefieldgenerator
  qdoc
  qgltf
  qhelpgenerator
  qlalr
  qmake
  qml
  qmlcachegen
  qmleasing
  qmlformat
  qmlimportscanner
  qmllint
  qmlmin
  qmlplugindump
  qmlpreview
  qmlprofiler
  qmlscene
  qmltestrunner
  qmltyperegistrar
  qscxmlc
  qtattributionsscanner
  qtdiag
  qtpaths
  qtplugininfo
  qtwaylandscanner
  qvkgen
  rcc
  repc
  sdpscanner
  tracegen
  uic
  xmlpatterns
  xmlpatternsvalidator

(venv) C:\Users\Administrator\PycharmProjects\pythonProject>

四、例子(基于对话框、基于窗口)

1、基于窗口

test.py     (pyuic5生成)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(350, 320, 75, 23))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))

main.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
import test     # module test.py


# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # print_hi('PyCharm')
    app = QApplication(sys.argv)
    myMainWindow = QMainWindow()

    myUi = test.Ui_MainWindow()

    myUi.setupUi(myMainWindow)
    myMainWindow.show()
    sys.exit(app.exec_())

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

2、基于对话框

untitled (对话框)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")

        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))

main.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QDialog
import untitled     # module untitled.py
from PyQt5.QtCore import *      # for Qt.ApplicationModal
if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 因为使用Qt Designer设计的ui是默认继承自object类,不提供show()显示方法,
    # 所以我们需要生成一个QDialog对象来重载我们设计的Ui_Dialog类,从而达到显示效果。
    MainDialog = QDialog()      # 创建一个主窗体(必须要有一个主窗体)
    myDialog = untitled.Ui_Dialog()   # 创建对话框
    myDialog.setupUi(MainDialog)   # 将对话框依附于主窗体
    # 设置窗口的属性为ApplicationModal模态,用户只有关闭弹窗后,才能关闭主界面
    # MainDialog.setWindowModality(Qt.ApplicationModal)
    MainDialog.show()
    sys.exit(app.exec_())

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyCharm 2020.1是集成开发环境(IDE),主要用于编写和调试Python代码。它具有许多强大的功能,例如代码自动补全、调试器、版本控制集成等,使开发人员更加高效地编写代码。 PyQt是一个用于创建图形用户界面(GUI)应用程序的Python库。它是对Qt应用程序框架的Python绑定,提供了丰富的界面组件和功能。通过PyQt,开发人员可以轻松地创建跨平台的GUI应用程序。 Qt Designer是Qt开发工具包的一部分,它是一个可视化设计工具,用于创建Qt应用程序的用户界面。它提供了丰富的界面组件和布局选项,开发人员可以通过拖放和设置属性来设计界面。Qt Designer还可以将设计的界面转换为Python代码,以便在PyQt中使用。 在PyCharm 2020.1中使用PyQt和Qt Designer的案例可以是创建一个简单的GUI应用程序。首先,我们可以在PyCharm中创建一个新的PyQt项目,然后使用Qt Designer来设计应用程序的界面。在Qt Designer中,我们可以添加按钮、标签、文本框等界面组件,并设置它们的属性和布局。 然后,我们可以将设计好的界面保存为.ui文件,并将其转换为Python代码。在PyCharm中,我们可以使用PyQt的工具来将.ui文件转换为.py文件,并在代码中导入生成的Python模块。 接下来,我们可以在PyCharm中编写代码来处理界面组件的事件,例如按钮的点击事件。通过PyQt提供的信号与槽机制,我们可以连接界面组件的信号和事件处理函数,以实现交互逻辑。 最后,我们可以在PyCharm中运行项目,测试和调试应用程序的功能。PyCharm提供了调试器和代码分析工具,帮助我们找出潜在的错误并进行修复。 总之,使用PyCharm 2020.1、PyQt和Qt Designer,我们可以方便地开发跨平台的GUI应用程序,通过可视化设计界面和编写Python代码,使应用程序更加易于维护和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值