说明
1、Python编程语言与Qt库的成功融合,是制作GUI应用程序的工具包。
QT库是目前最强大的库之一。PyQT库文件由C++编写,QT软件用于C++界面开发;QT是C++Python是Python的移植版,PyQt是Qtwrapper。
2、PyQT5是一种在底层实现窗口基本功能的先进语言。
实例
importsys fromPyQT5importQtCore,QtGui,QtWidgets fromPyQT5.QtWidgetsimport* fromPyQT5.QtCoreimport* fromDialog.HttpLoginimportHttpPost classUi_dialog(QWidget): def__init__(self): super().__init__() self.setupUi()#界面绘制交给Initui的方法 defsetupUi(self): self.setObjectName("学生登录界面") self.resize(535,344) font=QtGui.QFont() font.setFamily("Arial") font.setPointSize(14) #注意变量定义的顺序 self.lineEdit=QtWidgets.QLineEdit(self) self.lineEdit.setGeometry(QtCore.QRect(190,100,231,31)) self.lineEdit.setObjectName("lineEdit") #将其设置为密码输入框 self.lineEdit_2=QtWidgets.QLineEdit(self) self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password) self.lineEdit_2.setGeometry(QtCore.QRect(190,150,231,31)) self.lineEdit_2.setObjectName("lineEdit_2") #禁止窗户变化(顶部设置) self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint) self.setFont(font) self.pushButton=QtWidgets.QPushButton(self) self.pushButton.setGeometry(QtCore.QRect(100,280,93,28)) self.pushButton.setObjectName("pushButton") #取消按钮添加响应事件 self.pushButton.clicked.connect(QCoreApplication.instance().quit) #确认按钮 self.pushButton_2=QtWidgets.QPushButton(self) self.pushButton_2.setGeometry(QtCore.QRect(340,280,93,28)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_2.clicked.connect(self.forlogin)#默认执行带参数没有参数,点击执行 self.label=QtWidgets.QLabel(self) self.label.setGeometry(QtCore.QRect(90,100,72,21)) self.label.setObjectName("label") self.label_2=QtWidgets.QLabel(self) self.label_2.setGeometry(QtCore.QRect(90,155,72,20)) self.label_2.setObjectName("label_2") self.retranslateUi(self) QtCore.QMetaObject.connectSlotsByName(self) defretranslateUi(self,dialog): _translate=QtCore.QCoreApplication.translate dialog.setWindowTitle(_translate("dialog","Dialog")) self.pushButton.setText(_translate("dialog","取消")) self.pushButton_2.setText(_translate("dialog","确认")) self.label.setText(_translate("dialog","账号:")) self.label_2.setText(_translate("dialog","密码:")) defforlogin(self): #获取帐户输入框和密码输入框的类容 name=self.lineEdit.text() password=self.lineEdit_2.text() sign=HttpPost.login(name,password) ifsign=="true": print("登录成功") self.QCoreApplication.instance().quit#关闭当前窗口 else: print("登录失败") reply=QtWidgets.QMessageBox.question(self,"警告","请重新登录密码错误",QtWidgets.QMessageBox.Yes,QtWidgets.QMessageBox.No) ifreply==QtWidgets.QMessageBox.Yes: self.lineEdit_2.setText("") self.lineEdit.setText("") else: self.QCoreApplication.instance().quit#关闭当前窗口 if__name__='__main__': app=QApplication(sys.argv) my=Ui_dialog() my.show() sys.exit(app.exec_())
以上是python PyQT5实现窗口功能的方法,希望对大家有所帮助。更多Python学习指导:python基础教程
本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。