简体   繁体   English

PyQt:从LineEdit创建变量

[英]PyQt : Creating variables from LineEdit

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_())

I am trying to take user input from the 4 LineEdit boxes in my GUI and use them as variables in my function for the web services call. 我试图从GUI的4个LineEdit框中获取用户输入,并将它们用作Web服务调用函数中的变量。 But i am receiving this error. 但我收到此错误。

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

ed

This is my first app so go easy thanks. 这是我的第一个应用程序,所以轻松一点。

Any help would be greatly appreciated. 任何帮助将不胜感激。

William 威廉

You have to attach you reference to ths widgets to the objects, using self : 您必须使用self将对这些小部件的引用附加到对象上:

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

Also, you need to pass the text value of the widget, not the widget reference: 另外,您需要传递窗口小部件的文本值,而不是窗口小部件引用:

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

Like this these references become attributes to your object and can be referenced later in any method by using self 像这样,这些引用成为对象的属性,以后可以在任何方法中使用self进行引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM