Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/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 获取组合框以设置显示的小部件?_Python_Pyqt_Pyqt4_Qwidget_Qcombobox - Fatal编程技术网

Python 获取组合框以设置显示的小部件?

Python 获取组合框以设置显示的小部件?,python,pyqt,pyqt4,qwidget,qcombobox,Python,Pyqt,Pyqt4,Qwidget,Qcombobox,我正在为我编写的python程序制作一个GUI,希望根据组合框的选择,让一个区域显示不同的小部件 这是一个MWE: 我希望此主窗口中的轮廓区域显示TextWidget或CalendarWidget,具体取决于comboBox是否设置为选项1或选项2 所有GUI都是使用Qt设计器构建的,python文件使用uic加载它们 import sys from PyQt4 import QtCore, QtGui, uic Main = "Main.ui" MainUI, MainBase = ui

我正在为我编写的python程序制作一个GUI,希望根据组合框的选择,让一个区域显示不同的小部件

这是一个MWE:

我希望此主窗口中的轮廓区域显示TextWidget或CalendarWidget,具体取决于comboBox是否设置为选项1或选项2

所有GUI都是使用Qt设计器构建的,python文件使用uic加载它们

import sys
from PyQt4 import QtCore, QtGui, uic

Main = "Main.ui"
MainUI, MainBase = uic.loadUiType(Main)
Text = "textwidget.ui"
TWidget, TBase = uic.loadUiType(Text)
Cal = "calendarwidget.ui"
CWidget, CBase = uic.loadUiType(Cal)

class OperatorGUI(MainBase, MainUI):
    def __init__(self, parent=None):
        super(OperatorGUI, self).__init__(parent)
        self.setupUi(self)
        self.comboBox.activated[str].connect(self.choose_widget)


    def choose_widget(self, choice):
        # Set the widget accorging to the choice
        #if choice == "Option 1":
        print choice


class TextWidget(TBase, TWidget):
    def __init__(self):
        # Something
        print "Text"

class CalendarWidget(CBase, CWidget):
    def __init__(self):
        # Something
        print "Calendar"


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = OperatorGUI()
    window.show()
    sys.exit(app.exec_())
如果我使用QtDockWidget,我无法将其放置在我想要的位置。我不希望它是可拆卸的或弹出在一个单独的窗口

我怎样才能解决这个问题

编辑

在@eyllanesc提供了一些帮助之后,代码现在如下所示:

import sys
from PyQt4 import QtCore, QtGui, uic

Main = "Main.ui"
MainUI, MainBase = uic.loadUiType(Main)
Text = "textwidget.ui"
TWidget, TBase = uic.loadUiType(Text)
Cal = "calendarwidget.ui"
CWidget, CBase = uic.loadUiType(Cal)

class OperatorGUI(MainBase, MainUI):
    def __init__(self, parent=None):
        super(OperatorGUI, self).__init__(parent)
        self.setupUi(self)
        self.comboBox.activated[str].connect(self.choose_widget)

        self.ChangingWidget.setLayout(QtGui.QVBoxLayout())
        self.stacked = QtGui.QStackedWidget()
        self.ChangingWidget.layout().addWidget(self.stacked)
        self.textWidget = TextWidget()
        self.calendarWidget = CalendarWidget()
        self.stacked.addWidget(self.textWidget)
        self.stacked.addWidget(self.calendarWidget)


    def choose_widget(self, choice):
        # Set the widget accorging to the choice
        if choice == "Option 1":
            self.stacked.setCurrentWidget(self.textWidget)
        elif choice == "Option 2":
            self.stacked.setCurrentWidget(self.calendarWidget)
        print choice
        self.setupUi(self)
        self.show()


class TextWidget(TBase, TWidget):
    def __init__(self, parent=None):
        super(TextWidget, self).__init__(parent)
        self.setParent(parent)
        print "Text"

class CalendarWidget(CBase, CWidget):
    def __init__(self, parent=None):
        super(CalendarWidget, self).__init__(parent)
        self.setParent(parent)
        print "Calendar"


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = OperatorGUI()
    window.show()
    sys.exit(app.exec_())

为了完成此任务,建议使用,为此,我假设可以通过self.widget访问与该区域关联的小部件:

class OperatorGUI(MainBase, MainUI):
    def __init__(self, parent=None):
        super(OperatorGUI, self).__init__(parent)
        self.setupUi(self)
        self.comboBox.activated[str].connect(self.choose_widget)

        self.widget.setLayout(QVBoxLayout())
        self.stacked = QtGui.QStackedWidget()
        self.widget.layout().addWidget(self.stacked)
        self.textWidget = TextWidget()
        self.calendarWidget = CalendarWidget()
        self.stacked.addWidget(self.textWidget)
        self.stacked.addWidget(self.calendarWidget)

    def choose_widget(self, choice):
        # Set the widget accorging to the choice
        if choice == "Option 1":
            self.stacked.setCurrentWidget(self.textWidget)
        elif choice == "Option 2":
            self.stacked.setCurrentWidget(self.calendarWidget)

@Kajsa在第一个图像中,QComboBox下面有一个框,它是什么类型,它的名称是什么?在.ui中,它是一个QWidget,但我得到的错误没有属性“addWidget”。因此,我假设我需要使用另一种类型,只要我知道要使用哪种类型,我就可以轻松地使用。然后我得到的错误没有属性“addLayout”。@Kajsa这是一个拼写错误,我已经更正了,它必须是setLayout:P@Kajsa如果有效,请不要忘记将其标记为正确在choose_widget.Done上Premove self.setupUi self,但仍然没有显示。您必须在CalendarWidget和TextWidget类中调用self.setupUi self。此外,self.setParentparent不是必需的,因为您已通过superTextWidget、self放置它。u init_uparents非常感谢您!我已经被这个问题困扰了好几天,无法找到任何我能理解的PyQt4包文档。你是救命恩人,我希望我能给你更多的放弃投票!