#!/usr/bin/python
# -*- coding: UTF-8 -*-
# QQ: 78619808
# Created by Kylin on 2017/5/31
import sys
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.setWindowTitle(u'加密字符串')
self.setFixedSize(300,200)
vbox=QVBoxLayout()
self.inputbox=QTextEdit()
vbox.addWidget(self.inputbox)
hbox=QHBoxLayout()
tranbtn=QPushButton(u'加密')
aboutbtn=QPushButton(u'关于')
self.resultLabel = QLabel("Result:")
hbox.addWidget(aboutbtn)
hbox.addWidget(tranbtn)
aboutbtn.clicked.connect(self.OnAbout)
tranbtn.clicked.connect(self.OnTran)
vbox.addLayout(hbox)
self.outputbox=QTextEdit()
vbox.addWidget(self.outputbox)
vbox.addWidget(self.resultLabel)
self.setLayout(vbox)
def OnAbout(self):
QMessageBox.about(self,u'关于',u'字符串加密工具 by 史艳文')
def OnTran(self):
url = self.inputbox.toPlainText()
if url.isEmpty(): #执行到这里出错了,退出了消息循环
self.resultLabel.setText("是空的")
self.resultLabel.setText("不是空的")
if __name__=='__main__':
app=QApplication(sys.argv)
myshow=Window()
myshow.show()
sys.exit(app.exec_())
After converting pyqt4 to pyqt5, it is OK to write url.isEmpty() like this in pyqt4, but an error occurs in pyqt5 (no error will be reported, but the message loop will exit). How to change it?
In PyQt4, the toPlainText method returns the QString class, and the QString class supports the isEmpty method. So in PyQt4 this is no problem.
Most of PyQt5 is used under Python3 (of course PyQt5+Python2 can also be used). The basic str class in Python3 already supports various character encodings, so there is no QString in PyQt5. All those who are looking forward to the QString type API, just use the native str directly. Similarly, the toPlainText method returns the native str type. str does not have an isEmpty method, so it will fail.
You can use the common str judgment method here