2016-07-05 74 views
0

下面脚本中的按钮应该在按下后打开一个消息框。但是在按下按钮之前,脚本已经打开了两个消息框。PyQT在按下按钮之前两次运行脚本

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

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

要调用'showDialog'在构造函数 –

+0

非常感谢,这工作得很好。从来没有人会明白我自己...大大appreaciated。 – rainer

回答

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