我是靠谱客的博主 帅气石头,这篇文章主要介绍PyQt+PyCharm,现在分享给大家,希望可以做个参考。

本文描述Windows系统下如何安装Python + PyCharm + PyQt5,并通过PyQt5 采用 两种方式设计GUI界面。a.直接使用代码设计界面;b. 先使用QtDesigner进行可视化设计,然后将生成的.ui文件转换成.py文件。

安装python(本人3.7)

安装PyQt

复制代码
1
2
3
pip install PyQt5 pip install PyQt5-tools

安装pyCharm(可自行破解)

配置
1,点击:File -》Settings
2,Tools -》 External Tools -》点击“+”号
3,设置Qt Designer
修改三个地方,其他地方默认:

复制代码
1
2
3
4
Name:Qt Designer Programs:C:UserscdAnaconda3Libsite-packagespyqt5_toolsdesigner.exe Working directory:$ProjectFileDir$

注意:Programs参数需要修改为你电脑里边的“designer.exe”路径。

4,配置PyUIC

设置四个地方,其他可以默认

复制代码
1
2
3
4
5
Name:PyUIC Programs:C:UserscdAnaconda3python.exe Parameters:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py Working directory:$ProjectFileDir$

注意:Programs参数需要修改为你电脑里边的python“python.exe”路径。

四、使用Qt Designer
1,完成以上步骤之后,点击 Tools -》External Tools -》Qt Designer 启动我们的Qt Designer。

2,启动后选择:Widget,建立空白的窗口,点击 Create,其他默认就行

3,从左边 1区 拖拽,注意是“拖拽”控件到工作区,修改对应属性

4,做完基本的界面设置之后,会看到同目录下生成了一个“.ui”的文件

5,右键 External Tools -》PyUIC ,将“.ui”文件转为“.py”文件

6,这时,如果一切正常,没有报错的话,会在同目录下生成对应的“.py”文件

7,将下面的代码,放到生成的“.py”文件,放到最后就行(注意缩进)

复制代码
1
2
3
4
5
6
7
8
9
if __name__=="__main__": import sys from PyQt5.QtGui import QIcon app=QtWidgets.QApplication(sys.argv) widget=QtWidgets.QWidget() ui=Ui_Form() ui.setupUi(widget) widget.setWindowIcon(QIcon('web.png'))#增加icon图标,如果没有图片

可以没有这句

复制代码
1
2
3
widget.show() sys.exit(app.exec_())

8,运行启动,开启了pythonGUI旅程

9,源代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'login.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(552, 288) self.username = QtWidgets.QLabel(Form) self.username.setGeometry(QtCore.QRect(90, 90, 48, 20)) self.username.setObjectName("username") self.username_2 = QtWidgets.QLabel(Form) self.username_2.setGeometry(QtCore.QRect(90, 130, 48, 20)) self.username_2.setObjectName("username_2") self.layoutWidget = QtWidgets.QWidget(Form) self.layoutWidget.setGeometry(QtCore.QRect(140, 130, 189, 22)) self.layoutWidget.setObjectName("layoutWidget") self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget) self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0) self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.lineEdit_2 = QtWidgets.QLineEdit(self.layoutWidget) self.lineEdit_2.setObjectName("lineEdit_2") self.horizontalLayout_2.addWidget(self.lineEdit_2) self.radioButton = QtWidgets.QRadioButton(Form) self.radioButton.setGeometry(QtCore.QRect(150, 180, 131, 16)) self.radioButton.setObjectName("radioButton") self.pushButton_2 = QtWidgets.QPushButton(Form) self.pushButton_2.setGeometry(QtCore.QRect(180, 220, 75, 23)) self.pushButton_2.setObjectName("pushButton_2") self.widget = QtWidgets.QWidget(Form) self.widget.setGeometry(QtCore.QRect(140, 90, 189, 22)) self.widget.setObjectName("widget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setObjectName("horizontalLayout") self.lineEdit = QtWidgets.QLineEdit(self.widget) self.lineEdit.setObjectName("lineEdit") self.horizontalLayout.addWidget(self.lineEdit) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.username.setText(_translate("Form", "用户名:")) self.username_2.setText(_translate("Form", "密码:")) self.radioButton.setText(_translate("Form", "记住用户名和密码")) self.pushButton_2.setText(_translate("Form", "登录")) if __name__ == "__main__": import sys from PyQt5.QtGui import QIcon app = QtWidgets.QApplication(sys.argv) widget = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(widget) widget.setWindowIcon(QIcon('web.png')) # 增加icon图标,如果没有图片可以没有这句 widget.show() sys.exit(app.exec_())

最后

以上就是帅气石头最近收集整理的关于PyQt+PyCharm的全部内容,更多相关PyQt+PyCharm内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(110)

评论列表共有 0 条评论

立即
投稿
返回
顶部