2013-01-19 138 views
0

我是新來的PyQt,我期待爲演示了一個簡單的按鈕,當其點擊將它打開,與QTextEdit一起一個新的小窗口代碼PyQt的上點擊新窗口打開

+1

http://stackoverflow.com/a/4839224/1110381 ... – l4mpi

+0

好,我試圖W1 =的QTextEdit()w1.show(),但沒有任何反應 – Zed

+0

你需要構建一個QApplication在此之前,如果您沒有在QApp上交互調用_exec以確保程序不會立即退出。但是我將這個特定答案聯繫起來的原因是關於模態對話的評論:http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qinputdialog.html#details – l4mpi

回答

12

這裏是什麼入手:

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 

from PyQt4 import QtCore, QtGui 

class MyDialog(QtGui.QDialog): 
    def __init__(self, parent=None): 
     super(MyDialog, self).__init__(parent) 

     self.buttonBox = QtGui.QDialogButtonBox(self) 
     self.buttonBox.setOrientation(QtCore.Qt.Horizontal) 
     self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) 

     self.textBrowser = QtGui.QTextBrowser(self) 
     self.textBrowser.append("This is a QTextBrowser!") 

     self.verticalLayout = QtGui.QVBoxLayout(self) 
     self.verticalLayout.addWidget(self.textBrowser) 
     self.verticalLayout.addWidget(self.buttonBox) 

class MyWindow(QtGui.QWidget): 
    def __init__(self, parent=None): 
     super(MyWindow, self).__init__(parent) 

     self.pushButtonWindow = QtGui.QPushButton(self) 
     self.pushButtonWindow.setText("Click Me!") 
     self.pushButtonWindow.clicked.connect(self.on_pushButton_clicked) 

     self.layout = QtGui.QHBoxLayout(self) 
     self.layout.addWidget(self.pushButtonWindow) 

     self.dialogTextBrowser = MyDialog(self) 

    @QtCore.pyqtSlot() 
    def on_pushButton_clicked(self): 
     self.dialogTextBrowser.exec_() 


if __name__ == "__main__": 
    import sys 

    app = QtGui.QApplication(sys.argv) 
    app.setApplicationName('MyWindow') 

    main = MyWindow() 
    main.show() 

    sys.exit(app.exec_())