pyqt4 Slot SIGNAL 一个简单测试!

pyqt4 Slot SIGNAL 一个简单测试!

怎么写才能实现点击按钮然后退出程序呢??

[Copy to clipboard] [ - ]
CODE:
>>>import sys
>>>from PyQt4 import QtCore, QtGui
>>>app=QtGui.QApplication(sys.argv)
>>>win = QtGui.QPushButton("Hello Word",None)
>>>win.connect(win,QtCore.SIGNAL("clicked()"),win,QtCore.SLOT("close()"))
>>>win.show()
>>>app.exec_()

这个时候点击屏幕按钮就可以退出了

[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui

class HelloButton(QtGui.QPushButton):

        def __init__(self,parent=None):
                QtGui.QPushButton.__init__(self, parent)
                self.setText("Hello World")

class HelloWindow(QtGui.QMainWindow):

        def __init__(self,parent=None):
                QtGui.QMainWindow.__init__(self,parent)
                self.button=HelloButton(self)
                self.setCentralWidget(self.button)

def main(args):
        app=QtGui.QApplication(args)
        win=HelloWindow()
        win.connect(win,QtCore.SIGNAL("clicked()"),win,QtCore.SLOT("close()"))
        win.show()
        app.connect(app, QtCore.SIGNAL("lastWindowClosed()"),
                      app, QtCore.SLOT("quit()"))
        app.exec_()

if __name__=="__main__":
        main(sys.argv)

正确代码
win.connect(win,QtCore.SIGNAL("clicked()"),win,QtCore.SLOT("close()"))

下面是之前出现过的错误!
QtCore.SLOT("quit()"))
错误提示
Object::connect: No such slot QMainWindow::quit()

问了一下单位搞python的哥们帮我该了一下,不过还是不是很清楚!有点郁闷!

主要就是这句话
win.connect(win,QtCore.SIGNAL("clicked()"),win,QtCore.SLOT("close()"))

win本身就是一个button 为什么第一个对象还要写win.button 呢还有就是SIGNAL  和 SLOT 具体怎么用还是不理解!

win.connect(win,QtCore.SIGNAL("clicked()"),win,QtCore.SLOT("close()"))

希望拿给高手给解释解释SIGNAL 和 SLOT 的用法!

有错误信息吗?
运行正常我就是想点击“HelloButton ” 的时候退出程序,实际上我就是对这个signal 和slot  没搞懂怎么调用!
我在其中一行加了注释

[Copy to clipboard] [ - ]
CODE:
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created: Mon Aug 14 14:26:51 2006
#      by: PyQt4 UI code generator 4.0.1
#
# WARNING! All changes made in this file will be lost!

import sys
from PyQt4 import QtCore, QtGui

class Ui_gongzi(object):
    def setupUi(self, gongzi):
        gongzi.setObjectName("gongzi")
        gongzi.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(gongzi.minimumSizeHint()))

        self.verticalLayout = QtGui.QWidget(gongzi)
        self.verticalLayout.setGeometry(QtCore.QRect(20,120,160,80))
        self.verticalLayout.setObjectName("verticalLayout")

        self.vboxlayout = QtGui.QVBoxLayout(self.verticalLayout)
        self.vboxlayout.setMargin(0)
        self.vboxlayout.setSpacing(6)
        self.vboxlayout.setObjectName("vboxlayout")

        self.lineEdit = QtGui.QLineEdit(self.verticalLayout)
        self.lineEdit.setObjectName("lineEdit")
        self.vboxlayout.addWidget(self.lineEdit)

        self.verticalLayout_2 = QtGui.QWidget(gongzi)
        self.verticalLayout_2.setGeometry(QtCore.QRect(20,20,160,80))
        self.verticalLayout_2.setObjectName("verticalLayout_2")

        self.vboxlayout1 = QtGui.QVBoxLayout(self.verticalLayout_2)
        self.vboxlayout1.setMargin(0)
        self.vboxlayout1.setSpacing(6)
        self.vboxlayout1.setObjectName("vboxlayout1")

        self.label = QtGui.QLabel(self.verticalLayout_2)
        self.label.setObjectName("label")
        self.vboxlayout1.addWidget(self.label)

        self.verticalLayout_3 = QtGui.QWidget(gongzi)
        self.verticalLayout_3.setGeometry(QtCore.QRect(260,130,111,71))
        self.verticalLayout_3.setObjectName("verticalLayout_3")

        self.vboxlayout2 = QtGui.QVBoxLayout(self.verticalLayout_3)
        self.vboxlayout2.setMargin(0)
        self.vboxlayout2.setSpacing(6)
        self.vboxlayout2.setObjectName("vboxlayout2")

        self.pushButton = QtGui.QPushButton(self.verticalLayout_3)
        self.pushButton.setObjectName("pushButton")
        self.vboxlayout2.addWidget(self.pushButton)

        self.retranslateUi(gongzi)
        QtCore.QMetaObject.connectSlotsByName(gongzi)

    def retranslateUi(self, gongzi):
        gongzi.setWindowTitle(QtGui.QApplication.translate("gongzi", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit.setText(QtGui.QApplication.translate("gongzi", "1111111111", None, QtGui.QApplication.UnicodeUTF8))
#        self.label.setText(QtGui.QApplication.translate("gongzi", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("gongzi", "PushButton", None, QtGui.QApplication.UnicodeUTF8))

下面是一个可执行的程序,目的就是把你输入的东西显示出来!

[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/env python
#! -*- coding: gb2312 -*-
import sys
from PyQt4 import QtCore, QtGui

from ui_gongzi import Ui_gongzi

class Gongzi(QtGui.QWidget):
        def __init__(self, parent=None):
                QtGui.QWidget.__init__(self, parent)
                self.ui = Ui_gongzi()
                self.ui.setupUi(self)
               
#                self.ui.pushButton.setObjectName("pushButton1")
# 调用类的事件
#                self.ui.pushButton.connect(self.ui.pushButton,
#                        QtCore.SIGNAL("clicked()"),
#                        self,QtCore.SLOT("close()"))

# 按按钮以后把lineEdit 的内容显示到lable
                self.ui.pushButton.connect(self.ui.pushButton,
                        QtCore.SIGNAL("clicked()"),self.printa)


# lineEdit 取输入的字段用下面的操作
# a = QtCore.QString(win.text())
                self.value = QtCore.QString(self.ui.lineEdit.text())
# lable 输出内容
#                self.ui.label.setText(QtGui.QApplication.translate(self.value)
                self.ui.label.setText(QtGui.QApplication.translate("gongzi",
                        "%s" % self.value, None, QtGui.QApplication.UnicodeUTF8))
               

        def printa(self):
                print "%s" % self.value
                self.value = QtCore.QString(self.ui.lineEdit.text())
                self.ui.label.setText(QtGui.QApplication.translate("gongzi",
                   "%s" % self.value, None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        gongzi = Gongzi()
#        gongzi.ui.pushButton.connect(gongzi.ui.pushButton,
#                        QtCore.SIGNAL("clicked()"),
#                        gongzi.ui.pushButton,QtCore.SLOT("close"))
        gongzi.show()
        sys.exit(app.exec_())

测了一下还能用,不过语句写的很烂!将就能用!