簡介
pyqt5是qt的Python版本,因為最近需要做一個有界面的程式,所以想到這個庫,這裡就稍微介紹它的安裝和使用教程
1.安裝qt5
可能需要安裝vs的c++編譯組件
pip install pyQt5
2.使用拖拽組件編寫頁面
使用此工具打開組件
ctrl+s 生成.ui文件
2.UI文件生成py文件
pyuic5 -o a.py a.ui
3.完整代碼
Main.py
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from a_ui import Ui_Dialog # 假设您的UI类定义在a_ui.py文件中
class MyDialog(QtWidgets.QDialog):
def __init__(self):
super(MyDialog, self).__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
# 连接按钮的点击信号到槽函数
self.ui.pushButton.clicked.connect(self.on_pushButton_clicked)
def on_pushButton_clicked(self):
# 获取输入框的文本
text = self.ui.lineEdit.text()
print(f"输入的文本是: {text}")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
dialog = MyDialog()
dialog.show()
sys.exit(app.exec_())
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'a.ui'
#
# Created by: PyQt5 UI code generator 5.15.10
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(30, 20, 151, 20))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(210, 20, 75, 24))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept) # type: ignore
self.buttonBox.rejected.connect(Dialog.reject) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "確認"))