【问题标题】:PyQt : Creating variables from LineEditPyQt:从 LineEdit 创建变量
【发布时间】:2013-02-14 14:38:11
【问题描述】:
import logging
import sys
import suds
from PyQt4 import QtCore, QtGui, QtNetwork
from service import Ui_Form
from suds.wsse import *

logging.basicConfig(level=logging.DEBUG)
logging.getLogger("suds.client").setLevel(logging.CRITICAL)
url = "http://xxxxxxxxx:xxxx/services/FireScopeConfigurationWebService/v1?wsdl"
token = UsernameToken("xxx", "xxxxx")
security = Security()
security.tokens.append(token)

client = suds.client.Client(url)
client.set_options(wsse = security)

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.createButton,QtCore.SIGNAL("clicked()"), self.create_srv)
        ciEditLine = QtGui.QLineEdit()        #variable 1
        str(ciEditLine.displayText())
        monitorEditLine = QtGui.QLineEdit()   #variable 2
        str(monitorEditLine.displayText())
        bolEditLine = QtGui.QLineEdit()       #variable 3
        str(bolEditLine.displayText())
        ipEditLine = QtGui.QLineEdit()        #variable 4
        str(ipEditLine.displayText())

    def create_srv(self):
        try:
                response = client.service.createConfigurationItem(ciEditLine, monitorEditLine, bolEditLine, ipEditLine)
                print response
        except WebFault, e:
                print e


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

我正在尝试从我的 GUI 中的 4 个 LineEdit 框中获取用户输入,并将它们用作我的函数中的变量以进行 Web 服务调用。但是我收到了这个错误。

Traceback (most recent call last):
  File "start.py", line 34, in create_srv
    str(ciEditLine.displayText())
NameError: global name 'ciEditLine' is not defined

编辑

这是我的第一个应用程序,所以请放心,谢谢。

任何帮助将不胜感激。

威廉

【问题讨论】:

  • 您的小部件到底在哪里?不应该是 self.ui.ciEditLine 吗?

标签: python qt soap pyqt


【解决方案1】:

您必须使用self 将您对这些小部件的引用附加到对象:

def __init__(self):
    ...
    self.ciEditLine = QtGui.QLineEdit()        #variable 1
    ...

另外,您需要传递小部件的文本值,而不是小部件引用:

def create_srv(self):
    try:
            response = client.service.createConfigurationItem(self.ciEditLine.text(), self.monitorEditLine.text(), self.bolEditLine.text(), self.ipEditLine.text())

像这样,这些引用成为对象的属性,以后可以使用self在任何方法中引用

【讨论】:

  • 谢谢,这似乎奏效了。变量未传递到 XML 客户端请求中。我可以看到它在 0x2138950 处具有 <PyQt4.QtGui.QLineEdit 对象作为变量...?
  • @WilliamFleming 我更新了代码。您需要通过调用小部件的 text() 方法来传递文本值。传递小部件引用而不是其文本值会导致您播种的小部件引用的表示形式。
猜你喜欢
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
相关资源
最近更新 更多