Python 点击打开新窗口
我是PyQT新手,我正在寻找一个演示简单按钮的代码,单击该按钮将打开一个新的小窗口,其中包含QTextEditPython 点击打开新窗口,python,pyqt,Python,Pyqt,我是PyQT新手,我正在寻找一个演示简单按钮的代码,单击该按钮将打开一个新的小窗口,其中包含QTextEdit #!/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)
#!/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_())
…嗯,我尝试了w1=QTextEdit w1.show,但没有发生任何问题。在此之前,您需要构造一个QApplication,如果您没有以交互方式运行,请在QApp上调用_exec,以确保程序不会立即退出。但我将这个具体答案联系起来的原因是关于模态对话框的评论: