Tkinter是python內置的GUI實現模塊,但需要手動去搭建組件,調整樣式。PyQt實現了類似Qt creator的功能,可以直接在界面通過拖動的方式調整類似於按鈕、輸入框等,很大的簡化了使用。鑑於自己安裝的是python2.7,所以使用PyQt4版本。
1. 安裝
1.1 python2.7
1.2 開發環境 Pycharm
1.3 PyQt4
PyQt4 下載鏈接 https://sourceforge.net/projects/pyqt/files/PyQt4/
PyQt4 Python2.7 Windows *64版 下載鏈接https://sourceforge.net/projects/pyqt/files/PyQt4/PyQt-4.9.4/
也可以pip install (如果你安裝上的話,反正博主pip 是死活成功不了)
(PyQt5 下載鏈接https://sourceforge.net/projects/pyqt/files/PyQt5/)
如下圖:一定選好版本
2. 配置
打開Pycharm,File ->Settings ->Tools ->External Tools,添加擴展組件
! 注意PyUIC 這個操作是有中間的Arguments的!
-m PyQt4.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
添加完成後,在Tools ->External Tools查看是否添加成功
3、點擊QtDesigner,會跳出Qt Designer設計界面
此時就可以在該窗口進行GUI設計了
設計完成UI界面後,保存UI文件到文件夾路徑, 這個路徑很重要,不要保存錯了,不然後面轉換的時候,會找不到UI文件路徑 我是在工程中新建了一個UI文件夾,專門存放.ui文件 注意:這個路徑和上面那個Py uic的配置的Working Directory的路徑要保持一致
UI文件轉Py文件 右鍵UI文件選擇Py UIC 然後會自動生成一個Py文件
4、最後-代碼調用自動生成的.py UI文件
這個是生成的.py代碼
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui-a.ui'
#
# Created: Wed Jun 12 17:14:40 2019
# by: PyQt4 UI code generator 4.9.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_firstMainWindow(object):
def setupUi(self, firstMainWindow):
firstMainWindow.setObjectName(_fromUtf8("firstMainWindow"))
firstMainWindow.resize(556, 444)
firstMainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.centralwidget = QtGui.QWidget(firstMainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.queren = QtGui.QPushButton(self.centralwidget)
self.queren.setGeometry(QtCore.QRect(430, 360, 93, 28))
self.queren.setObjectName(_fromUtf8("queren"))
firstMainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(firstMainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 556, 23))
self.menubar.setObjectName(_fromUtf8("menubar"))
firstMainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(firstMainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
firstMainWindow.setStatusBar(self.statusbar)
self.retranslateUi(firstMainWindow)
QtCore.QMetaObject.connectSlotsByName(firstMainWindow)
def retranslateUi(self, firstMainWindow):
firstMainWindow.setWindowTitle(QtGui.QApplication.translate("firstMainWindow", "UI設計師", None, QtGui.QApplication.UnicodeUTF8))
self.queren.setText(QtGui.QApplication.translate("firstMainWindow", "確認", None, QtGui.QApplication.UnicodeUTF8))
if __name__=="__main__":
import sys
reload(sys) #
sys.setdefaultencoding( "utf-8" ) # 這兩步是爲了解決print中文報ascii編碼錯誤
app=QtGui.QApplication(sys.argv)
widget= QtGui.QMainWindow()
ui= Ui_firstMainWindow()
ui.setupUi(widget)
widget.show()
sys.exit(app.exec_())
一定在最下面加入以下代碼,然後運行,即可看到效果
if __name__=="__main__":
import sys
reload(sys) #
sys.setdefaultencoding( "utf-8" ) # 這兩步是爲了解決print中文報ascii編碼錯誤
app=QtGui.QApplication(sys.argv)
widget= QtGui.QMainWindow()
ui= Ui_firstMainWindow()
ui.setupUi(widget)
widget.show()
sys.exit(app.exec_())
5、最後,需要把py文件轉換成exe執行程序文件
把py文件和圖標(必須要 .ico 格式的,可以在線轉)放在同一個文件下
cd 到相應的目錄下,執行:
pyinstaller -F -i 1.ico 2.py 或
pyinstaller.exe -w -F xx\xx\xxx.py
(默認運行時有終端黑窗口,加 -m 會取消黑窗口)
-w: 直接發佈的exe應用帶命令行調試窗口,在指令內加入-w命令可以屏蔽掉命令框(調試階段可不加-w, 最終發佈時加入-w參數)
-F: 這裏是大寫。使用-F指令可以把應用打包成一個獨立的exe文件,否則是一個帶各種dll和依賴文件的文件夾
-p :這個指令後面可以增加pyinstaller搜索模塊的路徑。因爲應用打包涉及的模塊很多。這裏可以自己添加路徑。不過經過筆者測試,site-packages目錄下都是可以被識別的,一般不需要再手動添加
-i 指令
表示可執行文件的圖標,後面跟着圖標文件
執行完成後,在dist文件下會生成exe文件,點擊執行