PyQT按钮单击连接不会关闭应用程序

时间:2019-03-13 11:40:11

标签: python qt pyqt4

PyQT按钮单击“连接”不会关闭应用程序。

我正在尝试在Windows平台上编写一个示例应用程序,在该应用程序上,单击按钮应关闭该应用程序,然后会弹出GUI,但在按钮上单击,则无任何作用,并且未显示任何错误。

从pyuic4.bat生成的将.ui文件转换为.py文件的代码:

class Ui_Test(object):
def setupUi(self, Test):
    Test.setObjectName(_fromUtf8("Test"))
    Test.resize(637, 439)
    self.centralWidget = QtGui.QWidget(Test)
    self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
    self.pushButton = QtGui.QPushButton(self.centralWidget)
    self.pushButton.setGeometry(QtCore.QRect(240, 60, 151, 71))
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    Test.setCentralWidget(self.centralWidget)
    self.menuBar = QtGui.QMenuBar(Test)
    self.menuBar.setGeometry(QtCore.QRect(0, 0, 637, 21))
    self.menuBar.setObjectName(_fromUtf8("menuBar"))
    Test.setMenuBar(self.menuBar)
    self.mainToolBar = QtGui.QToolBar(Test)
    self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
    Test.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
    self.statusBar = QtGui.QStatusBar(Test)
    self.statusBar.setObjectName(_fromUtf8("statusBar"))
    Test.setStatusBar(self.statusBar)

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

def retranslateUi(self, Test):
    Test.setWindowTitle(_translate("Test", "Test", None))
    self.pushButton.setText(_translate("Test", "Click", None))


#if __name__ == "__main__":
#import sys
#app = QtGui.QApplication(sys.argv)
#Test = QtGui.QMainWindow()
#ui = Ui_Test()
#ui.setupUi(Test)
#Test.show()
#sys.exit(app.exec_())

我将类Ui_Test继承到另一个py文件中,如下所示:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QApplication, QCursor, QMainWindow, QPushButton
from ui_test import Ui_Test

class Test(QtGui.QWidget):

def __init__(self, parent=None):
    super(Test, self).__init__(parent)
    self.setupUi(self)
    self.pushButton.clicked.connect(self.cancel_dialog)

def cancel_dialog(self):
    print ("Closing Dialog Window...")
    self.close()

if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = QMainWindow()
ui = Ui_Test()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())

请让我知道如何纠正此问题。

0 个答案:

没有答案