位置: IT常识 - 正文
推荐整理分享python PyQt5如何实现窗口功能,希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!
说明
1、PyQt是制作GUI应用程序的工具包。Python编程语言与Qt库的成功融合。
Qt库是目前最强大的库之一。PyQt库文件由C++编写,Qt软件用于C++界面开发;Qt是C++库,PyQt是Python的移植版,PyQt是Qt的wrapper。
2、PyQt5是一种先进的语言,在底层实现了窗口的基本功能。
实例
importsysfromPyQt5importQtCore,QtGui,QtWidgetsfromPyQt5.QtWidgetsimport*fromPyQt5.QtCoreimport*fromDialog.HttpLoginimportHttpPostclassUi_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.translatedialog.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基础教程
友情链接: 武汉网站建设