PyQt5 安裝實測教學

PyQt5 安裝實測教學

1.安裝環境

首先使用Anaconda 的架構來安裝環境

# 創建一個叫做pyqt5的環境
conda create -n pyqt5 python=3.6 # 安裝pyqt5
pip install pyqt5 # 安裝pyqt5-tools(裡面有designer)
pip install pyqt5-tools # 安裝打包環境程式檔
pip install pyinstaller 

2.使用designer 建立UI檔案

安裝完後可以在以下的環境找到 designer.exe檔

D:\Anaconda_path\envs\pyqt5\Lib\site-packages\pyqt5_tools\Qt\bin

打開來會提供許多範本,隨便選一個創建一個新的UI介面

執行環境大概就如下圖所示,左邊有一排元件可以隨意的搭配使用,隨意拉取幾個元件後就可以把文件儲存下來



接下將檔案儲存成.ui附檔名

3.轉換UI檔案至py檔

再來可以使用內建的工具把剛剛創建的.ui檔轉成.py檔

pyuic6 your_qt_ui.ui -o output_name.py

底下就是程式自動將我們剛剛所排版的視窗化界面以程式的方式寫出來

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'UI_window.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(973, 719)
self.name = QtWidgets.QLineEdit(Dialog)
self.name.setGeometry(QtCore.QRect(150, 110, 171, 31))
self.name.setObjectName("name")
self.cell_phone = QtWidgets.QLineEdit(Dialog)
self.cell_phone.setGeometry(QtCore.QRect(150, 170, 171, 31))
self.cell_phone.setObjectName("cell_phone")
self.gender = QtWidgets.QComboBox(Dialog)
self.gender.setGeometry(QtCore.QRect(150, 240, 80, 22))
self.gender.setProperty("1", "")
self.gender.setProperty("2", "")
self.gender.setObjectName("gender")
self.gender.addItem("")
self.gender.addItem("")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(50, 110, 58, 15))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(50, 180, 58, 15))
self.label_2.setObjectName("label_2")
self.output_excel = QtWidgets.QPushButton(Dialog)
self.output_excel.setGeometry(QtCore.QRect(680, 600, 93, 28))
self.output_excel.setObjectName("output_excel")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.gender.setItemText(0, _translate("Dialog", "boy"))
self.gender.setItemText(1, _translate("Dialog", "girl"))
self.label.setText(_translate("Dialog", "Name"))
self.label_2.setText(_translate("Dialog", "Phone"))
self.output_excel.setText(_translate("Dialog", "Output excel"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

 

4.引用剛剛所建立的檔案

  • Dialog

    接下來引用剛剛所建立的檔案,若程式沒有出錯應該可以直接顯示出剛剛建立的視窗檔
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from UI_window import Ui_Dialog ## 此行隨著剛剛命名的文件而改(UI_window)
from PyQt5 import QtCore, uic, QtWidgets
class MyWindow(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MyWindow()
myWin.show()
sys.exit(app.exec_())

 

  • QMainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from stock_gui import Ui_MainWindow
class MyWindow(Ui_MainWindow,QMainWindow):
def __init__(self,parent = None):
super(MyWindow,self).__init__(parent)
self.setupUi(self)
self.login_b.clicked.connect(self.login_event)
self.test.clicked.connect(self.login_event)
def login_event(self):
print ("test")
print (self.account.text())
print (self.password.text())
self.label.setText("test")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QMainWindow()
ui = MyWindow()
ui.show()
sys.exit(app.exec_())

5.增加事件

假設今天在視窗格裡輸出一些資料,希望藉由按一個按鈕就把剛剛所輸入的資料都輸出成excel檔案,可以將程式碼加入以下事件

首先須找到剛剛建立button元件的名稱,然後加入click事件

self.output_excel.clicked.connect(self.output_event)

接著撰寫事件發生後的程式碼(self.output_event)

def output_event(self):
print ("test")
print (self.name.text())
print (self.name.text())
print (self.gender.currentText())
with open('output.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['姓名', '電話', '性別'])
writer.writerow([self.name.text(),self.name.text(), self.gender.currentText()])

 

底下是增加後的完整原始碼

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from UI_window import Ui_Dialog
from PyQt5 import QtCore, uic, QtWidgets
import csv
class MyWindow(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.output_excel.clicked.connect(self.output_event)
def output_event(self):
print ("test")
print (self.name.text())
print (self.name.text())
print (self.gender.currentText())
with open('output.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['姓名', '電話', '性別'])
writer.writerow([self.name.text(),self.name.text(), self.gender.currentText()])
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MyWindow()
myWin.show()
sys.exit(app.exec_())

6.打包成exe檔

接著使用剛剛安裝的pyinstall將程式碼打包成方便使用的exe檔,順利的話就可以在dist資料夾中找到剛剛包好的exe檔(要將D:Anaconda\envs\pyqt5\Lib\site-packages\PyQt5\Qt\bin 加入環境變數中)

pyinstaller -F -w My_first_qt.py

參考資料:

https://www.jianshu.com/p/d97dd23ff88a

https://www.jianshu.com/p/5b063c5745d0

https://blog.csdn.net/liubing8609/article/details/87474900

https://www.itread01.com/content/1528224970.html

https://blog.gtwang.org/programming/python-csv-file-reading-and-writing-tutorial/

https://zhuanlan.zhihu.com/p/48373518

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments