发布时间:2022-05-16 文章分类:Python 知识 投稿人:李佳 字号: 默认 | | 超大 打印

python PyQt5如何实现窗口功能

说明

1、PyQt是制作GUI应用程序的工具包。Python编程语言与Qt库的成功融合。

Qt库是目前最强大的库之一。PyQt库文件由C++编写,Qt软件用于C++界面开发;Qt是C++库,PyQt是Python的移植版,PyQt是Qt的wrapper。

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基础教程