无法在python中打印LineEdit值

时间:2015-09-23 16:45:32

标签: python forms printing qlineedit

enter image description here

enter image description here

class Ui_Form(QtWidgets.QWidget):

def __init__(self):

    super(self.__class__, self).__init__()
    self.setupUi(self)


def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(410, 325)
    font = QtGui.QFont()
    font.setBold(True)
    font.setUnderline(False)
    font.setWeight(75)
    Form.setFont(font)
    self.gridLayout = QtWidgets.QGridLayout(Form)
    self.gridLayout.setObjectName("gridLayout")
    self.verticalLayout = QtWidgets.QVBoxLayout()
    self.verticalLayout.setObjectName("verticalLayout")
    self.label = QtWidgets.QLabel(Form)
    font = QtGui.QFont()
    font.setPointSize(12)
    self.label.setFont(font)
    self.label.setObjectName("label")
    self.verticalLayout.addWidget(self.label)
    self.label_4 = QtWidgets.QLabel(Form)
    self.label_4.setObjectName("label_4")
    self.verticalLayout.addWidget(self.label_4)
    self.lineEdit_2 = QtWidgets.QLineEdit(Form)
    self.lineEdit_2.setObjectName("lineEdit_2")
    self.verticalLayout.addWidget(self.lineEdit_2)
    self.label_5 = QtWidgets.QLabel(Form)
    self.label_5.setObjectName("label_5")
    self.verticalLayout.addWidget(self.label_5)
    self.lineEdit = QtWidgets.QLineEdit(Form)
    self.lineEdit.setObjectName("lineEdit")
    self.verticalLayout.addWidget(self.lineEdit)     
    spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
    self.verticalLayout.addItem(spacerItem)
    self.horizontalLayout = QtWidgets.QHBoxLayout()
    self.horizontalLayout.setObjectName("horizontalLayout")
    self.pushButton_2 = QtWidgets.QPushButton(Form)
    self.pushButton_2.setObjectName("pushButton_2")
    self.horizontalLayout.addWidget(self.pushButton_2)
    self.pushButton = QtWidgets.QPushButton(Form)
    self.pushButton.setObjectName("pushButton")
    self.horizontalLayout.addWidget(self.pushButton)
    self.verticalLayout.addLayout(self.horizontalLayout)
    self.progressBar = QtWidgets.QProgressBar(Form)
    self.progressBar.setProperty("value", 0)
    self.progressBar.setObjectName("progressBar")
    self.verticalLayout.addWidget(self.progressBar)
    self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)

    self.pushButton_2.clicked.connect(self.Entrar)
    self.pushButton.clicked.connect(self.Cadastrar)


    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Gerenciador de Ordem de Serviço"))
    self.label.setText(_translate("Form", "Faça login ou cadastre-se"))
    self.label_4.setText(_translate("Form", "Login:"))
    self.label_5.setText(_translate("Form", "Senha:"))
    self.pushButton_2.setText(_translate("Form", "Entrar"))
    self.pushButton.setText(_translate("Form", "Cadastrar"))


def Entrar(self): #aqui faz a busca dos dados de cadastro, se corretos e cadastrados vai para programa principal
        log = self.lineEdit.text()
        passw = self.lineEdit_2.text()
        logqry = 'SELECT NomeUsu FROM registro WHERE NomeUsu ='  + "\""+ log +"\""
        passqry = 'SELECT SenhaProg FROM registro WHERE SenhaProg =' + "\"" + passw + "\""
        print(self.lineEdit.text())
        print(log)
        print(passw)
        print(logqry)
        print(passqry)

嗨,我有我的代码的这一部分,是一个gui,并且做了一个方法,它应该打印LineEdit和LineEdit_2值的值,但是当我运行事件时,那个不显示什么都没有,比如lineEdit是空的。

任何人都可以帮忙吗?对于任何语法错误感到抱歉。

1 个答案:

答案 0 :(得分:0)

logpassw不是普通的Python字符串,而是QString,因此要打印它们,您必须先将它们转换为字符串。

log = str(self.lineEdit.text())
passw = str(self.lineEdit_2.text())

或者,如果您希望它们为QString,请按以下步骤打印

print("%s\n%s" % log,passw)