如何在python中从QLineEdit中读出文本? [英] How to read out the text from QLineEdit in python?

查看:73
本文介绍了如何在python中从QLineEdit中读出文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的插件创建了一个带有 3 个按钮的启动 GUI.这非常有效,如果我单击其中一个按钮,则会启动特定操作.到目前为止,这是有效的.如果我单击其中一个按钮,则会出现一个带有确定"和取消"两个按钮的新 GUI,并且会出现一个 lineedit.如果我按下取消,GUI 将关闭,如果我按下确定,我希望程序从编辑行读取文本并将其存储在变量中.到目前为止,这不起作用.

I have created for my plugin a start GUI with 3 buttons. This works very well and if I click on one of the buttons a specific action is started. So far this works. If I click on one of the buttons a new GUI with two buttons "ok" and "cancel" and a lineedit appears. If I push on cancel the GUI will be closed, if I push on ok, I want the program to read the text from the editline and store it in a variable. This doesnßt work so far.

这是包含对话框的类:

from PyQt4.QtCore import pyqtSlot
from PyQt4.QtGui import QDialog, QLineEdit

from ui_grz import Ui_Dialog

class 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 结构的类:

This is the class that contians the structure of the GUI after creating the GUI with QT Designer and the command pyuic4:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class 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>", None, QtGui.QApplication.UnicodeUTF8))

在这个类中我需要变量:

And in this class I need the variable:

# Import the PyQt and QGIS libraries
from PyQt4.QtCore import * 
from PyQt4.QtGui import *
from qgis.core import *

# Import the code for the dialog
from ubgrzdialog import grzDialog

class 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 小部件读取文本有疑问的部分.

This is only one short part of the class, but this is the part where I have the question how to read the text from the LineEdit widget.

您有什么想法或建议吗?

Do you have any ideas or suggestions?

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

Thanks and sorry if this is a double post, but I haven´t found an appropriate answer for my problem.

推荐答案

文档QLineEdit 的文本可以通过它的方法text 检索.

As mentioned in the documentation, the text of a QLineEdit can be retrieved with its method text.

text = dlg.ui.lineEdit.text()

请注意,它是一个 QString,而不是常规字符串,但这应该不是问题,因为您使用 "%s" % text 对其进行格式化.

Note that it's a QString, not a regular string, but that shouldn't be a problem as you format it with your "%s" % text.

这篇关于如何在python中从QLineEdit中读出文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆