[英]PyQT: how to open new window
First of all, similar questions have been answered before, yet I need some help with this one.首先,之前已经回答过类似的问题,但我需要一些帮助。
I have a window which contains one button (Class First) and I want on pressed, a second blank window to be appeared (Class Second) .我有一个包含一个按钮(Class First)的窗口,我想在按下时出现第二个空白窗口(Class Second) 。
I fiddled with the code copied from this question: PyQT on click open new window , and I wrote this code:我摆弄了从这个问题复制的代码: PyQT on click open new window ,我写了这段代码:
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
import design1, design2
class Second(QtGui.QMainWindow, design2.Ui_MainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.setupUi(self)
class First(QtGui.QMainWindow, design1.Ui_MainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialog = Second(self)
def on_pushButton_clicked(self):
self.dialog.exec_()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
but on_pressed, this error message appears:但是 on_pressed 时,会出现此错误消息:
AttributeError: 'Second' object has no attribute 'exec_'
(design1 and design2 have been derived from the Qt designer.) (design1 和 design2 源自 Qt 设计器。)
Any thought would be appreciated.任何想法将不胜感激。
Here I'm using the show
method.这里我使用的是show
方法。
Here is a working example (derived from yours):这是一个工作示例(源自您的示例):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.pushButton = QtGui.QPushButton("click me")
self.setCentralWidget(self.pushButton)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialog = Second(self)
def on_pushButton_clicked(self):
self.dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If you need a new window every time you click the button, you can change the code that the dialog is created inside the on_pushButton_clicked
method, like so:如果每次单击按钮时都需要一个新窗口,则可以在on_pushButton_clicked
方法中更改创建对话框的代码,如下所示:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.pushButton = QtGui.QPushButton("click me")
self.setCentralWidget(self.pushButton)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialogs = list()
def on_pushButton_clicked(self):
dialog = Second(self)
self.dialogs.append(dialog)
dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.