在按下按钮之前,PyQT运行脚本两次

时间:2016-07-05 20:02:29

标签: pyqt messagebox

以下脚本中的按钮应该在按下后打开消息框。但是在按下按钮之前,脚本已经打开了两个消息框。

我试图用自我论证解决这个问题,但是脚本会打开两个框或者没有框。 任何帮助将不胜感激。

import sys
import subprocess
from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):

        super(MainWindow, self).__init__(parent)

        self.win_widget = WinWidget(self)
        widget = QtGui.QWidget()
        layout = QtGui.QVBoxLayout(widget)
        layout.addWidget(self.win_widget)
        self.setCentralWidget(widget)


        self.statusBar().showMessage('Ready')

        self.setGeometry(300, 300, 450, 250)
        self.setWindowTitle('Test')  
        self.setWindowIcon (QtGui.QIcon('logo.png'))
        self.show()

        self.win_widget = WinWidget (self)


class WinWidget (QtGui.QWidget) : 

    def __init__(self, parent): 
        super (WinWidget , self).__init__(parent)
        self.controls()
        self.grid_layout()
        self.showDialog()

    def controls(self):

        self.btn_file = QtGui.QPushButton('Choose Path  ', self)
        self.btn_file.setFont(QtGui.QFont('CourierNew', 12 ,  QtGui.QFont.Bold,False))
        self.btn_file.clicked.connect(self.showDialog)

        self.le_path = QtGui.QLineEdit("Python" ,self)
        self.le_path.setFont(QtGui.QFont('CourierNew' , 11))


    def grid_layout (self) : 
        grid = QtGui.QGridLayout()
        grid.setSpacing(2)
        grid.addWidget(self.btn_file , 1 , 0)
        grid.addWidget(self.le_path , 1, 1)
        self.setLayout(grid)

    # THE PART BELOW IS THE ONE THAT OPENS THE TWO BOXES BEFORE BUTTON IS PRESSED ...
    def showDialog (self) :
        self.msg = QtGui.QMessageBox(self)
        self.msg.setText("This is a message box")
        self.msg.setInformativeText("This is additional information")
        self.msg.setWindowTitle("MessageBox demo")
        self.msg.setDetailedText("The details are as follows:")
        self.msg.show()


def main():

    app = QtGui.QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

#try with this code. I did some changes.
#If your are not expecting this answer, sorry.

import sys
import subprocess
from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):

        super(MainWindow, self).__init__(parent)

        self.win_widget = WinWidget(self)
        widget = QtGui.QWidget()
        layout = QtGui.QVBoxLayout(widget)
        layout.addWidget(self.win_widget)
        self.setCentralWidget(widget)

        self.statusBar().showMessage('Ready')

        self.setGeometry(300, 300, 450, 250)
        self.setWindowTitle('Test')  
        self.setWindowIcon (QtGui.QIcon('logo.png'))
        #self.show()



class WinWidget (QtGui.QWidget) : 

    def __init__(self, parent): 
        super (WinWidget , self).__init__(parent)
        self.controls()
        self.grid_layout()
        self.showDialog()

    def controls(self):

        self.btn_file = QtGui.QPushButton('Choose Path  ', self)
        self.btn_file.setFont(QtGui.QFont('CourierNew', 12 ,  QtGui.QFont.Bold,False))
        self.btn_file.clicked.connect(self.showDialog)

        self.le_path = QtGui.QLineEdit("Python" ,self)
        self.le_path.setFont(QtGui.QFont('CourierNew' , 11))


    def grid_layout (self) : 
        grid = QtGui.QGridLayout()
        grid.setSpacing(2)
        grid.addWidget(self.btn_file , 1 , 0)
        grid.addWidget(self.le_path , 1, 1)
        self.setLayout(grid)

    # THE PART BELOW IS THE ONE THAT OPENS THE TWO BOXES BEFORE BUTTON IS PRESSED ...
    def showDialog (self) :
        self.msg = QtGui.QMessageBox(self)
        self.msg.setText("This is a message box")
        self.msg.setInformativeText("This is additional information")
        self.msg.setWindowTitle("MessageBox demo")
        self.msg.setDetailedText("The details are as follows:")
        #self.msg.show()
        self.msg.exec_() 

def main():

    app = QtGui.QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

#Thanks, 
#Subin Gopi