是否要检查textbox1是否为空?如何通过参考?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class My_Process(QWidget):
def __init__(self,*args):
self.textbox = QLineEdit()
def checkstatus(self):
if self.textbox.text() =="":
print("Empty Value Not Allowed")
self.textbox.setFocus()
else:
print(" Your Text : ",textbox.text())
#----------------------------------------------------------------------------------------------
class Mainprg(QMainWindow):
def __init__(self):
super().__init__()
self.tb = My_Process(self)
self.text1 = QLineEdit(self)
self.text2 = QLineEdit(self)
self.text1.setGeometry(100,100,300,30)
self.text2.setGeometry(100,200,300,30)
self.text1.editingFinished.connect(self.tb.checkstatus)
self.showMaximized()
app = QApplication(sys.argv)
window = Mainprg()
sys.exit(app.exec_())
对我来说,创建一个新类来进行验证令人困惑。我会做checkstatus
一个Mainprg方法:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Mainprg(QMainWindow):
def __init__(self):
super().__init__()
self.text1 = QLineEdit(self)
self.text2 = QLineEdit(self)
self.setGeometry(300, 200, 600, 450)
self.text1.setGeometry(100,100,300,30)
self.text2.setGeometry(100,200,300,30)
self.text1.editingFinished.connect(self.checkstatus)
self.show()
def checkstatus(self):
if self.text1.text() == "":
print("Empty Value Not Allowed")
self.text1.setFocus()
else:
print(" Your Text : ", self.text1.text())
app = QApplication(sys.argv)
window = Mainprg()
sys.exit(app.exec_())
上面的代码可行,但您可能希望为多个QLineEdit提供更通用的功能。没有真正好的方法可以将参数传递给PyQt connect语句,因此大多数人都使用lambda:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Mainprg(QMainWindow):
def __init__(self):
super().__init__()
self.text1 = QLineEdit(self)
self.text2 = QLineEdit(self)
self.setGeometry(300, 200, 600, 450)
self.text1.setGeometry(100,100,300,30)
self.text2.setGeometry(100,200,300,30)
self.text1.editingFinished.connect(lambda: self.checkstatus(self.text1))
self.text2.editingFinished.connect(lambda: self.checkstatus(self.text2))
self.show()
def checkstatus(self, widget):
if widget.text() == "":
print("Empty Value Not Allowed")
widget.setFocus()
else:
print(" Your Text : ", widget.text())
app = QApplication(sys.argv)
window = Mainprg()
sys.exit(app.exec_())
这段代码可以按照您希望的方式工作,但是如果text1为空并且尝试移至text2,则会遇到问题。您将获得“超出了最大递归深度”,因为该checkstatus
方法被反复调用。为防止这种情况,您必须执行以下任一操作:
setValidator()
如果您决定上新课,可以尝试以下方法:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class My_Process():
def __init__(self, widget):
self.textbox = widget
def checkstatus(self):
if self.textbox.text() == "":
print("Empty Value Not Allowed")
self.textbox.setFocus()
else:
print(" Your Text : ", self.textbox.text())
class Mainprg(QMainWindow):
def __init__(self):
super().__init__()
self.text1 = QLineEdit(self)
self.text2 = QLineEdit(self)
self.process = My_Process(self.text1)
self.setGeometry(300, 200, 600, 450)
self.text1.setGeometry(100,100,300,30)
self.text2.setGeometry(100,200,300,30)
self.text1.editingFinished.connect(self.process.checkstatus)
self.show()
app = QApplication(sys.argv)
window = Mainprg()
sys.exit(app.exec_())
要传递text1
给该类,请在init方法中添加一个参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句