【问题标题】:Simple PyQt4 showing other windows显示其他窗口的简单 PyQt4
【发布时间】:2014-07-07 05:39:01
【问题描述】:

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

以下是我尝试运行但无法弄清楚为什么新小部件未显示的代码。

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

请帮忙!

【问题讨论】:

  • 他们可能在函数结束时收集了垃圾。尝试使它们成为实例属性。将simple 更改为self.simple,另一个也一样。还有另一个没有函数show_anotherex = Example() 可能是错字。
  • 是的,实际上有点想通了,是的,示例是一个错字,也已修复。感谢您的帮助。

标签: python user-interface widget pyqt pyqt4


【解决方案1】:

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

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

请注意:如果您在 Widget 仍处于打开状态时尝试启动 Another(),您将收到错误消息:

QWidget::setLayout: 试图在另一个“”上设置 QLayout“”,它已经有一个布局

【讨论】:

  • 那么,如果我想在第一个窗口已经打开的情况下打开一个新窗口,我该怎么办???
  • 我会将在 Another() 类中设置网格布局的代码移动到 init 函数(这将停止错误,因为 init 仅在初始化对象时运行) .然后从你想要调用另一个的地方,只需用一个新名称初始化它,例如 self.another_2 = Another() 或者你可以创建一个类数组,然后根据需要添加和删除它们。
猜你喜欢
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
相关资源
最近更新 更多