PyQT:如何打开新窗口 [英] PyQT: how to open new window

查看:90
本文介绍了PyQT:如何打开新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,之前已经回答过类似的问题,但我需要一些帮助.

First of all, similar questions have been answered before, yet I need some help with this one.

我有一个包含一个按钮的窗口(Class First),我想在按下时出现第二个空白窗口(Class Second).

I have a window which contains one button (Class First) and I want on pressed, a second blank window to be appeared (Class Second).

我摆弄了从这个问题复制的代码:PyQT on click open new window,我写了这段代码:

I fiddled with the code copied from this question: PyQT on click open new window, and I wrote this code:

# -*- 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()  

但是on_pressed,出现这个错误信息:

but on_pressed, this error message appears:

AttributeError: 'Second' object has no attribute 'exec_'

(design1 和 design2 源自 Qt 设计器.)

任何想法将不胜感激.

推荐答案

这里我使用的是 show 方法.

Here I'm using the show method.

这是一个工作示例(源自您的示例):

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()

如果你每次点击按钮都需要一个新窗口,你可以在 on_pushButton_clicked 方法中更改创建对话框的代码,如下所示:

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:

#!/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()

这篇关于PyQT:如何打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆