Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 显示其他窗口的简单PyQt4_Python_User Interface_Widget_Pyqt_Pyqt4 - Fatal编程技术网

Python 显示其他窗口的简单PyQt4

Python 显示其他窗口的简单PyQt4,python,user-interface,widget,pyqt,pyqt4,Python,User Interface,Widget,Pyqt,Pyqt4,我正在尝试制作一个简单的小部件,其中有三个按钮,单击每个按钮时将显示相应的其他小部件 下面是我试图运行的代码,但我不明白为什么没有显示新的小部件 from PyQt4 import QtGui, QtCore from functools import partial import sys class MainWidget(QtGui.QWidget): def __init__(self): super(MainWidget, self).__init__()

我正在尝试制作一个简单的小部件,其中有三个按钮,单击每个按钮时将显示相应的其他小部件

下面是我试图运行的代码,但我不明白为什么没有显示新的小部件

from PyQt4 import QtGui, QtCore
from functools import partial
import sys


class MainWidget(QtGui.QWidget):
    def __init__(self):
       super(MainWidget, self).__init__()

       self.main_widget()

    def main_widget(self):
        another = Another()
        simple = Simple()
        grid = QtGui.QGridLayout()

        show_another_button = QtGui.QPushButton("Show Another")
        show_another_button.clicked.connect(another.show_another)
        grid.addWidget(show_another_button, 0, 0)

        show_simple_button = QtGui.QPushButton("Show Simple")
        show_simple_button.clicked.connect(simple.show_simple)
        grid.addWidget(show_simple_button, 0, 1)

        print_button = QtGui.QPushButton("Print Hello")
        print_button.clicked.connect(partial(print, "Hello"))
        grid.addWidget(another_button, 0, 2)

        self.setLayout(grid)

        self.show()


class Another(QtGui.QWidget):
    def __init__(self):
        print("another initialized")
        super(Another, self).__init__()

    def show_another(self):
        print("another called")
        grid = QtGui.QGridLayout()

        self.setLayout(grid)

        self.show()


class Simple(QtGui.QDialog):
    def __init__(self):
        print("simple initialized")
        super(Simple, self).__init__()

    def show_simple(self):
        print("simple called")
        self.show()

def main():
    app = QtGui.QApplication(sys.argv)
    main_widget = MainWidget()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

请帮忙

稍微修改了代码。以下内容适用于我的系统

from PyQt4 import QtGui
from functools import partial
import sys


class MainWidget(QtGui.QWidget):
    def __init__(self):
        super(MainWidget, self).__init__()

        self.another = Another()
        self.simple = Simple()
        self.grid = QtGui.QGridLayout()

        self.main_widget()

    def main_widget(self):
        show_another_button = QtGui.QPushButton("Show Another")
        show_another_button.clicked.connect(self.another.show_form)
        self.grid.addWidget(show_another_button, 0, 0)

        show_simple_button = QtGui.QPushButton("Show Simple")
        show_simple_button.clicked.connect(self.simple.show_simple)
        self.grid.addWidget(show_simple_button, 0, 1)

        another_button = QtGui.QPushButton("Print Hello")
        another_button.clicked.connect(partial(print, "Hello"))
        self.grid.addWidget(another_button, 0, 2)

        self.setLayout(self.grid)


class Another(QtGui.QWidget):
    def __init__(self):
        super(Another, self).__init__()
        print("another initialized")

    def show_form(self):
        print("another called")
        grid = QtGui.QGridLayout()

        self.setLayout(grid)

        self.show()


class Simple(QtGui.QDialog):
    def __init__(self):
        super(Simple, self).__init__()
        print("simple initialized")

    def show_simple(self):
        print("simple called")
        self.show()


def main():
    app = QtGui.QApplication(sys.argv)
    ex = MainWidget()
    ex.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
请注意:如果您尝试在小部件仍处于打开状态时启动另一个(),则会出现错误:


QWidget::setLayout:尝试在另一个“”上设置QLayout“”,该“”已具有布局

它们可能在函数结束时收集了垃圾。尝试使它们成为实例属性。将
simple
更改为
self.simple
,另一个也一样。另外,另一个没有函数
show\u另一个
ex=Example()
可能是一个打字错误。是的,事实上,我有点明白了,是的,Example是一个打字错误,并且也修复了它。Thanx的帮助。那么,如果我想在第一个窗口已经打开的情况下打开一个新窗口,我该怎么办呢???我会将另一个()类中设置网格布局的代码移到init函数中(这将停止错误,因为只有在初始化对象时才会运行init)。然后从你想调用另一个类的地方,用一个新名称初始化它,例如self.other_2=other(),或者你可以创建一个类数组,根据需要添加和删除它们。