我为我编写的python程序制作了一个GUI,并希望某个区域显示不同的widget
,具体取决于comboBox
的选择。
这是 MWE :
我希望此主窗口中的轮廓区域显示TextWidget
或CalendarWidget
,具体取决于comboBox
是设置为Option 1
还是Option 2
< / p>
所有GUI都是使用Qt Designer构建的,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_())
答案 0 :(得分:1)
要执行此任务,建议使用QStackedWidget
,为此我假设可以通过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)