PySide,连接PushButton错误

时间:2013-02-14 00:33:11

标签: qt pyside

我正在尝试在PySide中创建一个简单的测试应用程序,我真的不明白我错过了什么。这是迄今为止的代码:

    import sys
from PySide import QtCore, QtGui

class IPTest(QtGui.QMainWindow):
    def __init__(self):
        super(BartonTest, self).__init__()
        self.initUI()

    def initUI(self):
        lblAddress = QtGui.QLabel("IP Address", self)
        lineAddress = QtGui.QLineEdit(self)
        lblPort = QtGui.QLabel("Port Number", self)
        linePort = QtGui.QLineEdit(self)
        btnSend = QtGui.QPushButton("Send", self)
        btnReceive = QtGui.QPushButton("Receive", self)

        lblAddress.move(30, 20)
        lblPort.move(30, 60)
        lineAddress.move(130, 20)
        linePort.move(130, 60)
        btnSend.move(30, 100)
        btnReceive.move(130, 100)




        self.setGeometry(200, 200, 275, 150)
        self.setWindowTitle('Send/Receive TCP Test Program')
        self.show()

    def sendData(self):
        fileName, _ = QtGui.QFileDialog.getOPenFileName(self, 'Open CNC Program')
        self.data = open(fileName, 'r')



def main():
    app = QtGui.QApplication(sys.argv)
    bt = IPTest()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

现在我想做的只是将一个事件连接到按钮。 Qt的文档告诉我,我需要做的就是:

btnSend.clicked.connect(self.sendData)

PyCharm说它找不到点击的引用,我得到的例外是

TypeError: native Qt signal is not callable

我很容易(很容易)难倒。

1 个答案:

答案 0 :(得分:0)

以下对我有效:

import sys
from PySide import QtCore, QtGui

class IPTest(QtGui.QMainWindow):
    def __init__(self):
        super(IPTest, self).__init__()
        self.initUI()

    def initUI(self):
        lblAddress = QtGui.QLabel("IP Address", self)
        lineAddress = QtGui.QLineEdit(self)
        lblPort = QtGui.QLabel("Port Number", self)
        linePort = QtGui.QLineEdit(self)
        btnSend = QtGui.QPushButton("Send", self)
        btnReceive = QtGui.QPushButton("Receive", self)

        lblAddress.move(30, 20)
        lblPort.move(30, 60)
        lineAddress.move(130, 20)
        linePort.move(130, 60)
        btnSend.move(30, 100)
        btnReceive.move(130, 100)

        btnSend.clicked.connect(self.sendData)
        self.setGeometry(200, 200, 275, 150)
        self.setWindowTitle('Send/Receive TCP Test Program')
        self.show()

    def sendData(self):

        fileName, _ = QtGui.QFileDialog.getOpenFileName(self, 'Open CNC Program')
        if len(fileName) > 0:
            self.data = open(fileName, 'r')