如何在python中读出QLineEdit中的文本?

如何在python中读出QLineEdit中的文本?,第1张

概述我为我的插件创建了一个带有3个按钮的启动GUI.这非常有效,如果我单击其中一个按钮,则会启动特定 *** 作.到目前为止这个工作.如果我点击其中一个按钮,新的GUI有两个按钮“ok”和“cancel”,并出现一条lineedit.如果我按下取消,GUI将被关闭,如果我按下ok,我希望程序从editline读取文本并将其存储在变量中.到目前为止,这还没有奏效. 这是包含对话框的类: from PyQt4.Q 我为我的插件创建了一个带有3个按钮的启动GUI.这非常有效,如果我单击其中一个按钮,则会启动特定 *** 作.到目前为止这个工作.如果我点击其中一个按钮,新的GUI有两个按钮“ok”和“cancel”,并出现一条lineedit.如果我按下取消,GUI将被关闭,如果我按下ok,我希望程序从editline读取文本并将其存储在变量中.到目前为止,这还没有奏效.

这是包含对话框的类:

from PyQt4.QtCore import pyqtSlotfrom PyQt4.QtGui import QDialog,QlineEditfrom ui_grz import Ui_Dialogclass grzDialog(QDialog):    def __init__(self):        QDialog.__init__(self)        # Set up the user interface from Designer.        self.ui = Ui_Dialog()        self.ui.setupUi(self)

在使用QT Designer和命令pyuic4创建GUI之后,这是一个包含GUI结构的类:

from PyQt4 import QtCore,QtGuitry:    _fromUtf8 = QtCore.QString.fromUtf8except AttributeError:    _fromUtf8 = lambda s: sclass Ui_Dialog(object):    def setupUi(self,Dialog):        Dialog.setobjectname(_fromUtf8("Dialog"))        Dialog.resize(387,153)        self.buttonBox = QtGui.QDialogbuttonBox(Dialog)        self.buttonBox.setGeometry(QtCore.QRect(30,110,341,32))        self.buttonBox.setorIEntation(QtCore.Qt.Horizontal)        self.buttonBox.setStandardbuttons(QtGui.QDialogbuttonBox.Cancel|QtGui.QDialogbuttonBox.Ok)        self.buttonBox.setobjectname(_fromUtf8("buttonBox"))        self.label = QtGui.QLabel(Dialog)        self.label.setGeometry(QtCore.QRect(10,10,361,51))        self.label.setobjectname(_fromUtf8("label"))        self.lineEdit = QtGui.QlineEdit(Dialog)        self.lineEdit.setGeometry(QtCore.QRect(10,60,351,31))        self.lineEdit.setobjectname(_fromUtf8("lineEdit"))        self.retranslateUi(Dialog)        QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL(_fromUtf8("accepted()")),Dialog.accept)        QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL(_fromUtf8("rejected()")),Dialog.reject)        QtCore.QMetaObject.connectSlotsByname(Dialog)    def retranslateUi(self,Dialog):        Dialog.setwindowTitle(QtGui.QApplication.translate("Dialog","GRZ Analyse",None,QtGui.QApplication.UnicodeUTF8))        self.label.setText(QtGui.QApplication.translate("Dialog","<HTML><head/><body><p><span style=\" Font-weight:600;\">Bitte geben SIE hIEr den Schwellenwert für dIE GRZ-Analyse ein:</span></p><p>Bitte achten SIE auf eine korrekte Schreibweise (bspw. 2.5):</p></body></HTML>",QtGui.QApplication.UnicodeUTF8))

在这个课程中我需要变量:

# import the PyQt and QGIS librarIEsfrom PyQt4.QtCore import * from PyQt4.QtGui import *from qgis.core import *# import the code for the dialogfrom ubgrzdialog import grzDialogclass quickAnalysis:    def __init__(self,iface):        # Save reference to the QGIS interface        self.iface = iface    def grzAnalysis(self):        dlg = grzDialog()        dlg.show()        result = dlg.exec_()        if result == 1:            text = dlg.text()            QMessageBox.information(self.iface.mainWindow(),"test","%s" %(text),QMessageBox.Ok)

这只是该类的一小部分,但这是我有问题如何从lineEdit小部件中读取文本的部分.

你有什么想法或建议吗?

如果这是一个双重帖子,感谢和抱歉,但我找不到适合我的问题的答案.

解决方法 如 documentation中所述,可以使用其方法文本检索QlineEdit的文本.
text = dlg.ui.lineEdit.text()

请注意,它是一个QString,而不是常规字符串,但是当您使用“%s”%文本格式化它时,这不应该是一个问题.

总结

以上是内存溢出为你收集整理的如何在python中读出QLineEdit中的文本?全部内容,希望文章能够帮你解决如何在python中读出QLineEdit中的文本?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1207090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存