UI设计
- 1、在PyCharm中添加External Tools中添加QtDesigner和PyUIC工具
打开PyCharm>File>Settings>Tools>External Tools,单击+号添加工具,出现下图界面
Name:自己定义
Group:External Tools
Program:QtDesigner.exe所在位置
Working directory:$ProjectFileDir$
其他配置如上
Parameters:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
配置完成后会在Tools>External Tools 中显示出来添加的工具
- 2、启动QtDesigner,新建Main Window窗体
启动QtDesinger时,确保电源选项高级设置中可切换动态显卡全局设置为最佳性能,我的win10系统A卡显卡切换成最大化性能将会导致无法启动QtDesinger,具体原因不清楚
Qt5.7中集成QtCreator、QtDesinger,二者都可以创建UI文件,设计界面,本文使用后者,前者功能更强大,有兴趣的同学可以尝试
创建Main Window后,可以在左侧看到具体的窗口插件,Qt支持多种小插件,我还不怎么会使用。将插件拖到中间主体窗口,调整布局即可完成UI设计,右侧显示的是窗体属性和信号槽设计区,使用Qt的信号槽机制可以完成多种逻辑操作。Ctrl+r组合可以预览界面。
我自己设计的原始UI已经删除,下图是成品样子
- 3、UI设计完成后,直接使用PyUIC工具生成.py文件,在PyCharm中选中ui文件,然后点击运行PyUIC即可,以下代码自动生成。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.7
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(521, 555)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:\\Python35\\Lib\\site-packages\\cx_Freeze\\samples\\unti\\1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
mainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 510, 451, 16))
self.label.setText("")
self.label.setObjectName("label")
self.textBrowser_2 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_2.setGeometry(QtCore.QRect(230, 80, 251, 401))
self.textBrowser_2.setObjectName("textBrowser_2")
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(20, 0, 482, 80))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout.addWidget(self.pushButton_3)
self.pushButton_4 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout.addWidget(self.pushButton_4)
self.pushButton_5 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout.addWidget(self.pushButton_5)
self.pushButton_6 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton_6.setObjectName("pushButton_6")
self.horizontalLayout.addWidget(self.pushButton_6)
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_7.setGeometry(QtCore.QRect(41, 220, 75, 23))
self.pushButton_7.setObjectName("pushButton_7")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(40, 260, 161, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(40, 139, 161, 61))
self.layoutWidget.setObjectName("layoutWidget")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
self.label_2.setObjectName("label_2")
self.verticalLayout_2.addWidget(self.label_2)
self.lineEdit = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout_2.addWidget(self.lineEdit)
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(40, 490, 441, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
mainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(mainWindow)
self.pushButton_6.clicked.connect(mainWindow.close)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "词频统计"))
self.pushButton.setText(_translate("mainWindow", "打开文档"))
self.pushButton_2.setText(_translate("mainWindow", "统计词频"))
self.pushButton_3.setText(_translate("mainWindow", "批量查询"))
self.pushButton_4.setText(_translate("mainWindow", "清除所有"))
self.pushButton_5.setText(_translate("mainWindow", "保存结果"))
self.pushButton_6.setText(_translate("mainWindow", "退出程序"))
self.pushButton_7.setText(_translate("mainWindow", "查询"))
self.label_2.setText(_translate("mainWindow", "输入查询单词"))
添加图标icon.addPixmap(QtGui.QPixmap(path), QtGui.QIcon.Normal, QtGui.QIcon.Off)
UI中加入六个PushButton
组成第一排基本操作按钮
一个Label
提示输入查询单词
另一个Label
在最下方显示统计单词耗费的时间
UI右侧是一个Text Browser
显示统计结果
最下方还有一个进度条ProgressBar