【问题标题】:Call another GUI within a GUI在 GUI 中调用另一个 GUI
【发布时间】:2015-02-28 01:10:08
【问题描述】:

我有 2 个使用 qt-designer 创建的 GUI,并将其转换为 python - anmUi.py 和 anmInfoUI.py

虽然我可以通过运行以下命令导入并打开anmUi

import sys
sys.path.insert(0, '/user_data/test/anm/versions')
import anmTool_v01a
reload(anmTool_v01a)
win = anmTool_v01a.anmUi()
win.show()

但是,我在打开第二个 gui - anmInfoUI 时遇到了问题,这是由第一个 gui 中的按钮启动的。

我尝试使用与第一个 gui 类似的格式进行编写,但是当我尝试使用 .exec 运行它时,出现了诸如 # AttributeError: 'module' object has no attribute 'exec_' 之类的错误。

class anmUi(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self, parent = None, modal = False)
        self.ui = anmUi.Ui_Migration()
        self.ui.setupUi(self)
        self.createConnections()


    def createConnections(self):
        self.connect(self.ui.pushButton_editSelected, QtCore.SIGNAL('clicked()'), self.editSelected)


    def editSelected(self):
        selected_item = self.ui.treeWidget_migrateAnmg.selectedItems()
        if selected_item:
            anmInfoUI.exec_()


class anmInfoUI(QtGui.QDialog):

    def __init__(self, parent = None, modal = False):
        QtGui.QWidget.__init__(self, parent, modal = modal)

        self.ui = anmInfoUI.Ui_EditInfo()
        self.ui.setupUi(self)

如何让它运行?

【问题讨论】:

  • 请提供您调用exec_的代码,它不起作用(所有相关代码)。在任何人可以帮助您之前,有必要知道您在做什么
  • @three_pineapples 我已经编辑了我帖子中的代码,如上所示

标签: python user-interface pyqt


【解决方案1】:

exec_() 是你在 Qt 应用程序上调用的东西。在您的情况下,应用程序已经在运行(否则您根本看不到任何窗口),而您希望实例化新的小部件/窗口并调用其 show() 方法。

【讨论】:

  • 如果您包含更多的代码,重现问题的最小量,我们可以给您更准确的答案。
  • 使用 show() 对我不起作用...这是我的第一个 UI - pastebin.com/raw.php?i=88AuDBgc 的 Python 代码,而这是我的第二个 UI - pastebin.com/raw.php?i=PJ0ZKtrA 的 Python 代码。当我尝试使用show() 时,虽然我没有收到任何错误,但没有显示第二个用户界面
  • anmgTool 无法从您提供的代码中运行 ('QDialog' object has no attribute 'slot_cancel')
  • 抱歉回复晚了。好吧,我创建了一个名为“slot_cancel”的函数。也许你可以为它创建一个快速函数,或者你可以在 UI 代码中删除它。它应该以任何一种方式工作
【解决方案2】:

此代码成功打开新窗口。

class anmUi(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self, parent = None, modal = False)
        self.ui = Ui_Migration()
        self.ui.setupUi(self)
        self.createConnections()


    def createConnections(self):
        self.connect(self.ui.pushButton_editSelected, QtCore.SIGNAL('clicked()'), self.editSelected)


    def editSelected(self):
        selected_item = self.ui.treeWidget_migrateAnmg.selectedItems()
        if True:
            self.child = anmInfoUI()
            self.child.show()

    def slot_cancel(self, *args):
        pass


class anmInfoUI(QtGui.QDialog):

    def __init__(self, parent = None, modal = False):
        QtGui.QWidget.__init__(self, parent, modal = modal)

        self.ui = Ui_EditAsset()
        self.ui.setupUi(self)

注意 True 的使用(我不想填充您的选择框)。您可以使用其标志来控制新窗口是否为模态窗口。

【讨论】:

    猜你喜欢
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2020-01-28
    相关资源
    最近更新 更多