UI設計
- 1菠发、在PyCharm中添加External Tools中添加QtDesigner和PyUIC工具
打開PyCharm>File>Settings>Tools>External Tools塑猖,單擊+號添加工具角虫,出現(xiàn)下圖界面
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時将塑,確保電源選項高級設置中可切換動態(tài)顯卡全局設置為最佳性能,我的win10系統(tǒng)A卡顯卡切換成最大化性能將會導致無法啟動QtDesinger糕档,具體原因不清楚
Qt5.7中集成QtCreator、QtDesinger拌喉,二者都可以創(chuàng)建UI文件速那,設計界面,本文使用后者尿背,前者功能更強大端仰,有興趣的同學可以嘗試
創(chuàng)建Main Window后,可以在左側看到具體的窗口插件田藐,Qt支持多種小插件荔烧,我還不怎么會使用。將插件拖到中間主體窗口汽久,調整布局即可完成UI設計鹤竭,右側顯示的是窗體屬性和信號槽設計區(qū),使用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", "詞頻統(tǒng)計"))
self.pushButton.setText(_translate("mainWindow", "打開文檔"))
self.pushButton_2.setText(_translate("mainWindow", "統(tǒng)計詞頻"))
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
在最下方顯示統(tǒng)計單詞耗費的時間
UI右側是一個Text Browser
顯示統(tǒng)計結果
最下方還有一個進度條ProgressBar