打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
PyQt5初次体验

今天初次使用了PyQt5,感觉不错。
注意的问题是,PyQt5貌似只支持Python 3
碰到的问题:
没有找到QString类型,貌似是使用的Python的内建str类型。因此无法使用trimmed等方法,而要使用strip()方法。
connect方法和C++版本不一样。具体情况见代码。
在Qt5中,没有找到QVBoxLayout的setMargin方法,查找文档发现已经替换成了setContentsMargins方法。

OK,看一下代码吧:

#!/usr/bin/python3#coding:utf-8from PyQt5.QtWidgets import *import sysclass LoginDlg(QDialog):    def __init__(self, parent=None):        super(LoginDlg, self).__init__(parent)        usr = QLabel("用户:")        pwd = QLabel("密码:")        self.usrLineEdit = QLineEdit()        self.pwdLineEdit = QLineEdit()        self.pwdLineEdit.setEchoMode(QLineEdit.Password)        gridLayout = QGridLayout()        gridLayout.addWidget(usr, 0, 0, 1, 1)        gridLayout.addWidget(pwd, 1, 0, 1, 1)        gridLayout.addWidget(self.usrLineEdit, 0, 1, 1, 3);        gridLayout.addWidget(self.pwdLineEdit, 1, 1, 1, 3);        okBtn = QPushButton("确定")        cancelBtn = QPushButton("取消")        btnLayout = QHBoxLayout()        btnLayout.setSpacing(60)        btnLayout.addWidget(okBtn)        btnLayout.addWidget(cancelBtn)        dlgLayout = QVBoxLayout()        dlgLayout.setContentsMargins(40, 40, 40, 40)        dlgLayout.addLayout(gridLayout)        dlgLayout.addStretch(40)        dlgLayout.addLayout(btnLayout)        self.setLayout(dlgLayout)        okBtn.clicked.connect(self.accept)        cancelBtn.clicked.connect(self.reject)        self.setWindowTitle("登录")        self.resize(300, 200)    def accept(self):        if self.usrLineEdit.text().strip() == "eric" and self.pwdLineEdit.text() == "eric":            super(LoginDlg, self).accept()        else:            QMessageBox.warning(self,                    "警告",                    "用户名或密码错误!",                    QMessageBox.Yes)            self.usrLineEdit.setFocus()app = QApplication(sys.argv)dlg = LoginDlg()dlg.show()dlg.exec_()app.exit()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

效果如下:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
PyQt5(3)快速界面设计
pyqt中各种消息框的使用
【PyQt5
量化交易系统界面之pyqt5
【pyqtgraph绘图】在pyqtgraph中绘图
pyANSYS|包含GUI的示例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服