如何修改 QGroupBox 的 QLayout 以删除或添加参数 QTextEdit 输入? (Python, PyQt)
How do I modify a QLayout of a QGroupBox to remove or add parameter QTextEdit input? (Python, PyQt)
我正在尝试实现一个能够从用户那里获取参数的 window。在这个 window 中,用户应该给出行数(有 N 个参数集)。根据这个数字,我希望为每个 N
参数集自动生成 QTextEdit
集。所以从现在开始我有了这个代码
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from PyQt4 import QtGui
import sys
import numpy as np
class SurfViewer(QtGui.QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
# General Window
self.centralWidget = QtGui.QWidget()
self.setCentralWidget(self.centralWidget)
self.mainHBOX_param_scene = QtGui.QVBoxLayout()
# parameter2
self.horizontalGroupBox_Param2 = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
self.horizontalGroupBox_Param2.setMaximumHeight(80)
self.Param2_layout = QtGui.QVBoxLayout()
grid2 = QtGui.QGridLayout()
self.Param2_layout.addLayout(grid2)
self.Param2_Label = QtGui.QLabel('Name')
self.Param2 = QtGui.QComboBox()
self.Param2.addItems(["a","b","c"])
self.Param2.setMinimumWidth(100)
# self.Param2_layout.addWidget(self.Param2)
self.Param2_min_Label = QtGui.QLabel('min')
self.Param2_min_Edit = QtGui.QTextEdit('1')
self.Param2_min_Edit.setMaximumWidth(40)
self.Param2_max_Label = QtGui.QLabel('max')
self.Param2_max_Edit = QtGui.QTextEdit('5')
self.Param2_max_Edit.setMaximumWidth(40)
self.Param2_step_Label = QtGui.QLabel('step')
self.Param2_step_Edit = QtGui.QTextEdit('1')
self.Param2_step_Edit.setMaximumWidth(40)
grid2.addWidget(self.Param2_Label, 0, 0)
grid2.addWidget(self.Param2, 1, 0)
grid2.addWidget(self.Param2_min_Label, 0, 1)
grid2.addWidget(self.Param2_min_Edit, 1, 1)
grid2.addWidget(self.Param2_max_Label, 0, 2)
grid2.addWidget(self.Param2_max_Edit, 1, 2)
grid2.addWidget(self.Param2_step_Label, 0, 3)
grid2.addWidget(self.Param2_step_Edit, 1, 3)
self.horizontalGroupBox_Param2.setLayout(self.Param2_layout)
#action button
self.horizontalGroupBox_Actions = QtGui.QGroupBox("Actions")
self.horizontalGroupBox_Actions.setMaximumHeight(100)
self.layout_Actions = QtGui.QHBoxLayout()
self.Button_Run=QtGui.QPushButton('Run')
self.nbparam = QtGui.QTextEdit('2')
self.nbparam.setMaximumHeight(30)
self.nbparam.setMaximumWidth(50)
self.layout_Actions.addWidget(self.Button_Run)
self.layout_Actions.addWidget(self.nbparam)
self.horizontalGroupBox_Actions.setLayout(self.layout_Actions)
# set layout
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Actions)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Param2)
self.centralWidget.setLayout(self.mainHBOX_param_scene)
self.Button_Run.clicked.connect(self.Runclick)
def Runclick(self):
# parameter2
self.horizontalGroupBox_Param = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
nbparam = int(self.nbparam.toPlainText())
self.Param_layout = QtGui.QVBoxLayout()
grid2 = QtGui.QGridLayout()
self.Param_layout.addLayout(grid2)
self.Param_Label = QtGui.QLabel('Names')
self.Param_min_Label = QtGui.QLabel('mins')
self.Param_max_Label = QtGui.QLabel('maxs')
self.Param_step_Label = QtGui.QLabel('steps')
grid2.addWidget(self.Param_Label, 0, 0)
grid2.addWidget(self.Param_min_Label, 0, 1)
grid2.addWidget(self.Param_max_Label, 0, 2)
grid2.addWidget(self.Param_step_Label, 0, 3)
self.listofedit = []
text=["a","b","c"]
for i in np.arange(nbparam):
print(i)
self.listofedit.append([QtGui.QComboBox(),QtGui.QTextEdit('10'),QtGui.QTextEdit('50'),QtGui.QTextEdit('10')])
self.listofedit[i][0].addItems(text)
self.listofedit[i][0].setMinimumWidth(100)
self.listofedit[i][1].setMaximumWidth(40)
self.listofedit[i][2].setMaximumWidth(40)
self.listofedit[i][3].setMaximumWidth(40)
grid2.addWidget(self.listofedit[i][0], i+1, 0)
grid2.addWidget(self.listofedit[i][1], i+1, 1)
grid2.addWidget(self.listofedit[i][2], i+1, 2)
grid2.addWidget(self.listofedit[i][3], i+1, 3)
self.horizontalGroupBox_Param.setLayout(self.Param_layout)
# while self.horizontalGroupBox_Param2.layout().count():
# self.horizontalGroupBox_Param2.layout().itemAt(0).widget().deleteLater()
self.horizontalGroupBox_Param2.layout().clear()
self.horizontalGroupBox_Param2.setLayout(self.Param_layout)
return
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
哪个生成了这个 window :
所以我想要的是当我点击运行(右上角的数字是行数N)时,我修改window。所以在这种情况下,我应该有两行来输入两个名称、两个最小值、两个最大值和两个步长值。
我开始更新 window 但我正在努力寻找正确的代码放入 def 运行click(self):
我已经编写了代码来生成正确数量的参数集行,但是我无法用它们来更新我的 window。
我显然在做最后一部分写的东西:
self.horizontalGroupBox_Param.setLayout(self.Param_layout)
# while self.horizontalGroupBox_Param2.layout().count():
# self.horizontalGroupBox_Param2.layout().itemAt(0).widget().deleteLater()
self.horizontalGroupBox_Param2.setLayout(self.Param_layout)
我认为我没有以正确的方式处理事情。所以如果有人对此有解决方案,我将不胜感激
更新
您好@jgorosdev,感谢您的回答。
虽然,它并不完全符合我想要的。
在你给我的代码中,它工作正常,因为参数集总是在 window 的底部更新,但在我更高级的软件中,后面还有一些其他 QGroupBox。
例如,如果我添加一个控制台,如 part:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from PyQt4 import QtGui
import sys
import numpy as np
class SurfViewer(QtGui.QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
# General Window
self.centralWidget = QtGui.QWidget()
self.setCentralWidget(self.centralWidget)
self.mainHBOX_param_scene = QtGui.QVBoxLayout()
#action button
self.horizontalGroupBox_Actions = QtGui.QGroupBox("Actions")
self.horizontalGroupBox_Actions.setMaximumHeight(100)
self.layout_Actions = QtGui.QHBoxLayout()
self.Button_Run=QtGui.QPushButton('Run')
self.nbparam = QtGui.QTextEdit('3')
self.nbparam.setMaximumHeight(30)
self.nbparam.setMaximumWidth(50)
self.layout_Actions.addWidget(self.Button_Run)
self.layout_Actions.addWidget(self.nbparam)
self.horizontalGroupBox_Actions.setLayout(self.layout_Actions)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Actions)
self.centralWidget.setLayout(self.mainHBOX_param_scene)
self.updateWidget = None
# Param2 layout
self.horizontalGroupBox_Param2 = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
self.Param2_layout = QtGui.QVBoxLayout()
self.Button_Run.clicked.connect(self.UpdateLayout)
self.UpdateLayout()
# console
self.horizontalGroupBox_Console2 = QtGui.QGroupBox("Console ")
self.layout_Consol_Label2 = QtGui.QVBoxLayout()
self.Consol_Label2 = QtGui.QTextEdit('Console :')
self.layout_Consol_Label2.addWidget(self.Consol_Label2)
self.horizontalGroupBox_Console2.setLayout(self.layout_Consol_Label2)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Console2)
# parameter2
# From now on everything well be created inside a function that updates the layout
def UpdateLayout(self):
if (self.updateWidget == None):
self.updateWidget = QtGui.QWidget()
else:
self.mainHBOX_param_scene.removeWidget(self.updateWidget)
self.updateWidget.deleteLater()
self.updateWidget = None
cnt = self.mainHBOX_param_scene.layout().count()
print("Widget count:" +str(cnt)+ "")
nbparam = int(self.nbparam.toPlainText())
#self.horizontalGroupBox_Param2.setMaximumHeight(150)
grid2 = QtGui.QGridLayout()
self.Param2_Label = QtGui.QLabel('Name')
self.Param2 = QtGui.QComboBox()
self.Param2.addItems(["a","b","c"])
self.Param2.setMinimumWidth(100)
# self.Param2_layout.addWidget(self.Param2)
self.Param2_min_Label = QtGui.QLabel('min')
self.Param2_max_Label = QtGui.QLabel('max')
self.Param2_step_Label = QtGui.QLabel('step')
grid2.addWidget(self.Param2_Label, 0, 0)
grid2.addWidget(self.Param2_min_Label, 0, 1)
grid2.addWidget(self.Param2_max_Label, 0, 2)
grid2.addWidget(self.Param2_step_Label, 0, 3)
self.listofedit = []
text=["a","b","c"]
for i in np.arange(nbparam):
print(i)
self.listofedit.append([QtGui.QComboBox(),QtGui.QTextEdit('20'),QtGui.QTextEdit('50'),QtGui.QTextEdit('10')])
self.listofedit[i][0].addItems(text)
self.listofedit[i][0].setMinimumWidth(100)
self.listofedit[i][1].setMaximumWidth(40)
self.listofedit[i][1].setMaximumHeight(40)
self.listofedit[i][2].setMaximumWidth(40)
self.listofedit[i][2].setMaximumHeight(40)
self.listofedit[i][3].setMaximumWidth(40)
self.listofedit[i][3].setMaximumHeight(40)
grid2.addWidget(self.listofedit[i][0], i+1, 0)
grid2.addWidget(self.listofedit[i][1], i+1, 1)
grid2.addWidget(self.listofedit[i][2], i+1, 2)
grid2.addWidget(self.listofedit[i][3], i+1, 3)
self.updateWidget = QtGui.QWidget()
self.updateWidget.setLayout(grid2)
#self.updateWidget.setMaximumHeight(nbparam*60)
self.Param2_layout.addWidget(self.updateWidget)
self.horizontalGroupBox_Param2.setLayout(self.Param2_layout)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Param2)
return
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
开头有这个window:
这是我想要的,但我在点击 运行 按钮后得到了这张图片:
所以控制台和参数集是反的。
我想知道是否可以在不重新构建所有布局的情况下替换部分布局?
我对代码做了一些小改动,现在运行良好。
您在代码中创建了两次底部组框,我认为这是更好的方法。我已将所有内容打包到 UpdateLayout
函数中。
您可能需要重新检查调整 qt 大小的策略以正确调整主 window。
代码更新:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from PyQt4 import QtGui
import sys
import numpy as np
class SurfViewer(QtGui.QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
# General Window
self.centralWidget = QtGui.QWidget()
self.setCentralWidget(self.centralWidget)
self.mainHBOX_param_scene = QtGui.QVBoxLayout()
#action button
self.horizontalGroupBox_Actions = QtGui.QGroupBox("Actions")
self.horizontalGroupBox_Actions.setMaximumHeight(100)
self.layout_Actions = QtGui.QHBoxLayout()
self.Button_Run=QtGui.QPushButton('Run')
self.nbparam = QtGui.QTextEdit('3')
self.nbparam.setMaximumHeight(30)
self.nbparam.setMaximumWidth(50)
self.layout_Actions.addWidget(self.Button_Run)
self.layout_Actions.addWidget(self.nbparam)
self.horizontalGroupBox_Actions.setLayout(self.layout_Actions)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Actions)
self.centralWidget.setLayout(self.mainHBOX_param_scene)
self.updateWidget = None
# Param2 layout
self.horizontalGroupBox_Param2 = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
self.Param2_layout = QtGui.QVBoxLayout()
self.Button_Run.clicked.connect(self.UpdateLayout)
self.UpdateLayout()
# console
self.horizontalGroupBox_Console2 = QtGui.QGroupBox("Console ")
self.layout_Consol_Label2 = QtGui.QVBoxLayout()
self.Consol_Label2 = QtGui.QTextEdit('Console :')
self.layout_Consol_Label2.addWidget(self.Consol_Label2)
self.horizontalGroupBox_Console2.setLayout(self.layout_Consol_Label2)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Console2)
# parameter2
# From now on everything well be created inside a function that updates the layout
def UpdateLayout(self):
if (self.updateWidget == None):
self.updateWidget = QtGui.QWidget()
else:
self.mainHBOX_param_scene.removeWidget(self.updateWidget)
self.updateWidget.deleteLater()
self.updateWidget = None
cnt = self.mainHBOX_param_scene.layout().count()
print("Widget count:" +str(cnt)+ "")
nbparam = int(self.nbparam.toPlainText())
#self.horizontalGroupBox_Param2.setMaximumHeight(150)
grid2 = QtGui.QGridLayout()
self.Param2_Label = QtGui.QLabel('Name')
self.Param2 = QtGui.QComboBox()
self.Param2.addItems(["a","b","c"])
self.Param2.setMinimumWidth(100)
# self.Param2_layout.addWidget(self.Param2)
self.Param2_min_Label = QtGui.QLabel('min')
self.Param2_max_Label = QtGui.QLabel('max')
self.Param2_step_Label = QtGui.QLabel('step')
grid2.addWidget(self.Param2_Label, 0, 0)
grid2.addWidget(self.Param2_min_Label, 0, 1)
grid2.addWidget(self.Param2_max_Label, 0, 2)
grid2.addWidget(self.Param2_step_Label, 0, 3)
self.listofedit = []
text=["a","b","c"]
for i in np.arange(nbparam):
print(i)
self.listofedit.append([QtGui.QComboBox(),QtGui.QTextEdit('20'),QtGui.QTextEdit('50'),QtGui.QTextEdit('10')])
self.listofedit[i][0].addItems(text)
self.listofedit[i][0].setMinimumWidth(100)
self.listofedit[i][1].setMaximumWidth(40)
self.listofedit[i][1].setMaximumHeight(40)
self.listofedit[i][2].setMaximumWidth(40)
self.listofedit[i][2].setMaximumHeight(40)
self.listofedit[i][3].setMaximumWidth(40)
self.listofedit[i][3].setMaximumHeight(40)
grid2.addWidget(self.listofedit[i][0], i+1, 0)
grid2.addWidget(self.listofedit[i][1], i+1, 1)
grid2.addWidget(self.listofedit[i][2], i+1, 2)
grid2.addWidget(self.listofedit[i][3], i+1, 3)
self.updateWidget = QtGui.QWidget()
self.updateWidget.setLayout(grid2)
#self.updateWidget.setMaximumHeight(nbparam*60)
self.Param2_layout.addWidget(self.updateWidget,)
self.horizontalGroupBox_Param2.setLayout(self.Param2_layout)
#self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Param2)
# NEW
self.mainHBOX_param_scene.insertWidget(1,self.horizontalGroupBox_Param2)
return
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
对更新的回答
如果您愿意 updating QGroupBox 而不是更改位置,而不是 addWidget()
您需要的功能是 insertWidget()
函数。
我更新了 UpdateLayout
函数中添加小部件的行。希望适合你。
函数参考如下:
我正在尝试实现一个能够从用户那里获取参数的 window。在这个 window 中,用户应该给出行数(有 N 个参数集)。根据这个数字,我希望为每个 N
参数集自动生成 QTextEdit
集。所以从现在开始我有了这个代码
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from PyQt4 import QtGui
import sys
import numpy as np
class SurfViewer(QtGui.QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
# General Window
self.centralWidget = QtGui.QWidget()
self.setCentralWidget(self.centralWidget)
self.mainHBOX_param_scene = QtGui.QVBoxLayout()
# parameter2
self.horizontalGroupBox_Param2 = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
self.horizontalGroupBox_Param2.setMaximumHeight(80)
self.Param2_layout = QtGui.QVBoxLayout()
grid2 = QtGui.QGridLayout()
self.Param2_layout.addLayout(grid2)
self.Param2_Label = QtGui.QLabel('Name')
self.Param2 = QtGui.QComboBox()
self.Param2.addItems(["a","b","c"])
self.Param2.setMinimumWidth(100)
# self.Param2_layout.addWidget(self.Param2)
self.Param2_min_Label = QtGui.QLabel('min')
self.Param2_min_Edit = QtGui.QTextEdit('1')
self.Param2_min_Edit.setMaximumWidth(40)
self.Param2_max_Label = QtGui.QLabel('max')
self.Param2_max_Edit = QtGui.QTextEdit('5')
self.Param2_max_Edit.setMaximumWidth(40)
self.Param2_step_Label = QtGui.QLabel('step')
self.Param2_step_Edit = QtGui.QTextEdit('1')
self.Param2_step_Edit.setMaximumWidth(40)
grid2.addWidget(self.Param2_Label, 0, 0)
grid2.addWidget(self.Param2, 1, 0)
grid2.addWidget(self.Param2_min_Label, 0, 1)
grid2.addWidget(self.Param2_min_Edit, 1, 1)
grid2.addWidget(self.Param2_max_Label, 0, 2)
grid2.addWidget(self.Param2_max_Edit, 1, 2)
grid2.addWidget(self.Param2_step_Label, 0, 3)
grid2.addWidget(self.Param2_step_Edit, 1, 3)
self.horizontalGroupBox_Param2.setLayout(self.Param2_layout)
#action button
self.horizontalGroupBox_Actions = QtGui.QGroupBox("Actions")
self.horizontalGroupBox_Actions.setMaximumHeight(100)
self.layout_Actions = QtGui.QHBoxLayout()
self.Button_Run=QtGui.QPushButton('Run')
self.nbparam = QtGui.QTextEdit('2')
self.nbparam.setMaximumHeight(30)
self.nbparam.setMaximumWidth(50)
self.layout_Actions.addWidget(self.Button_Run)
self.layout_Actions.addWidget(self.nbparam)
self.horizontalGroupBox_Actions.setLayout(self.layout_Actions)
# set layout
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Actions)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Param2)
self.centralWidget.setLayout(self.mainHBOX_param_scene)
self.Button_Run.clicked.connect(self.Runclick)
def Runclick(self):
# parameter2
self.horizontalGroupBox_Param = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
nbparam = int(self.nbparam.toPlainText())
self.Param_layout = QtGui.QVBoxLayout()
grid2 = QtGui.QGridLayout()
self.Param_layout.addLayout(grid2)
self.Param_Label = QtGui.QLabel('Names')
self.Param_min_Label = QtGui.QLabel('mins')
self.Param_max_Label = QtGui.QLabel('maxs')
self.Param_step_Label = QtGui.QLabel('steps')
grid2.addWidget(self.Param_Label, 0, 0)
grid2.addWidget(self.Param_min_Label, 0, 1)
grid2.addWidget(self.Param_max_Label, 0, 2)
grid2.addWidget(self.Param_step_Label, 0, 3)
self.listofedit = []
text=["a","b","c"]
for i in np.arange(nbparam):
print(i)
self.listofedit.append([QtGui.QComboBox(),QtGui.QTextEdit('10'),QtGui.QTextEdit('50'),QtGui.QTextEdit('10')])
self.listofedit[i][0].addItems(text)
self.listofedit[i][0].setMinimumWidth(100)
self.listofedit[i][1].setMaximumWidth(40)
self.listofedit[i][2].setMaximumWidth(40)
self.listofedit[i][3].setMaximumWidth(40)
grid2.addWidget(self.listofedit[i][0], i+1, 0)
grid2.addWidget(self.listofedit[i][1], i+1, 1)
grid2.addWidget(self.listofedit[i][2], i+1, 2)
grid2.addWidget(self.listofedit[i][3], i+1, 3)
self.horizontalGroupBox_Param.setLayout(self.Param_layout)
# while self.horizontalGroupBox_Param2.layout().count():
# self.horizontalGroupBox_Param2.layout().itemAt(0).widget().deleteLater()
self.horizontalGroupBox_Param2.layout().clear()
self.horizontalGroupBox_Param2.setLayout(self.Param_layout)
return
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
哪个生成了这个 window :
所以我想要的是当我点击运行(右上角的数字是行数N)时,我修改window。所以在这种情况下,我应该有两行来输入两个名称、两个最小值、两个最大值和两个步长值。 我开始更新 window 但我正在努力寻找正确的代码放入 def 运行click(self):
我已经编写了代码来生成正确数量的参数集行,但是我无法用它们来更新我的 window。
我显然在做最后一部分写的东西:
self.horizontalGroupBox_Param.setLayout(self.Param_layout)
# while self.horizontalGroupBox_Param2.layout().count():
# self.horizontalGroupBox_Param2.layout().itemAt(0).widget().deleteLater()
self.horizontalGroupBox_Param2.setLayout(self.Param_layout)
我认为我没有以正确的方式处理事情。所以如果有人对此有解决方案,我将不胜感激
更新
您好@jgorosdev,感谢您的回答。 虽然,它并不完全符合我想要的。 在你给我的代码中,它工作正常,因为参数集总是在 window 的底部更新,但在我更高级的软件中,后面还有一些其他 QGroupBox。 例如,如果我添加一个控制台,如 part:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from PyQt4 import QtGui
import sys
import numpy as np
class SurfViewer(QtGui.QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
# General Window
self.centralWidget = QtGui.QWidget()
self.setCentralWidget(self.centralWidget)
self.mainHBOX_param_scene = QtGui.QVBoxLayout()
#action button
self.horizontalGroupBox_Actions = QtGui.QGroupBox("Actions")
self.horizontalGroupBox_Actions.setMaximumHeight(100)
self.layout_Actions = QtGui.QHBoxLayout()
self.Button_Run=QtGui.QPushButton('Run')
self.nbparam = QtGui.QTextEdit('3')
self.nbparam.setMaximumHeight(30)
self.nbparam.setMaximumWidth(50)
self.layout_Actions.addWidget(self.Button_Run)
self.layout_Actions.addWidget(self.nbparam)
self.horizontalGroupBox_Actions.setLayout(self.layout_Actions)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Actions)
self.centralWidget.setLayout(self.mainHBOX_param_scene)
self.updateWidget = None
# Param2 layout
self.horizontalGroupBox_Param2 = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
self.Param2_layout = QtGui.QVBoxLayout()
self.Button_Run.clicked.connect(self.UpdateLayout)
self.UpdateLayout()
# console
self.horizontalGroupBox_Console2 = QtGui.QGroupBox("Console ")
self.layout_Consol_Label2 = QtGui.QVBoxLayout()
self.Consol_Label2 = QtGui.QTextEdit('Console :')
self.layout_Consol_Label2.addWidget(self.Consol_Label2)
self.horizontalGroupBox_Console2.setLayout(self.layout_Consol_Label2)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Console2)
# parameter2
# From now on everything well be created inside a function that updates the layout
def UpdateLayout(self):
if (self.updateWidget == None):
self.updateWidget = QtGui.QWidget()
else:
self.mainHBOX_param_scene.removeWidget(self.updateWidget)
self.updateWidget.deleteLater()
self.updateWidget = None
cnt = self.mainHBOX_param_scene.layout().count()
print("Widget count:" +str(cnt)+ "")
nbparam = int(self.nbparam.toPlainText())
#self.horizontalGroupBox_Param2.setMaximumHeight(150)
grid2 = QtGui.QGridLayout()
self.Param2_Label = QtGui.QLabel('Name')
self.Param2 = QtGui.QComboBox()
self.Param2.addItems(["a","b","c"])
self.Param2.setMinimumWidth(100)
# self.Param2_layout.addWidget(self.Param2)
self.Param2_min_Label = QtGui.QLabel('min')
self.Param2_max_Label = QtGui.QLabel('max')
self.Param2_step_Label = QtGui.QLabel('step')
grid2.addWidget(self.Param2_Label, 0, 0)
grid2.addWidget(self.Param2_min_Label, 0, 1)
grid2.addWidget(self.Param2_max_Label, 0, 2)
grid2.addWidget(self.Param2_step_Label, 0, 3)
self.listofedit = []
text=["a","b","c"]
for i in np.arange(nbparam):
print(i)
self.listofedit.append([QtGui.QComboBox(),QtGui.QTextEdit('20'),QtGui.QTextEdit('50'),QtGui.QTextEdit('10')])
self.listofedit[i][0].addItems(text)
self.listofedit[i][0].setMinimumWidth(100)
self.listofedit[i][1].setMaximumWidth(40)
self.listofedit[i][1].setMaximumHeight(40)
self.listofedit[i][2].setMaximumWidth(40)
self.listofedit[i][2].setMaximumHeight(40)
self.listofedit[i][3].setMaximumWidth(40)
self.listofedit[i][3].setMaximumHeight(40)
grid2.addWidget(self.listofedit[i][0], i+1, 0)
grid2.addWidget(self.listofedit[i][1], i+1, 1)
grid2.addWidget(self.listofedit[i][2], i+1, 2)
grid2.addWidget(self.listofedit[i][3], i+1, 3)
self.updateWidget = QtGui.QWidget()
self.updateWidget.setLayout(grid2)
#self.updateWidget.setMaximumHeight(nbparam*60)
self.Param2_layout.addWidget(self.updateWidget)
self.horizontalGroupBox_Param2.setLayout(self.Param2_layout)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Param2)
return
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
开头有这个window:
这是我想要的,但我在点击 运行 按钮后得到了这张图片:
所以控制台和参数集是反的。 我想知道是否可以在不重新构建所有布局的情况下替换部分布局?
我对代码做了一些小改动,现在运行良好。
您在代码中创建了两次底部组框,我认为这是更好的方法。我已将所有内容打包到 UpdateLayout
函数中。
您可能需要重新检查调整 qt 大小的策略以正确调整主 window。
代码更新:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from PyQt4 import QtGui
import sys
import numpy as np
class SurfViewer(QtGui.QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
# General Window
self.centralWidget = QtGui.QWidget()
self.setCentralWidget(self.centralWidget)
self.mainHBOX_param_scene = QtGui.QVBoxLayout()
#action button
self.horizontalGroupBox_Actions = QtGui.QGroupBox("Actions")
self.horizontalGroupBox_Actions.setMaximumHeight(100)
self.layout_Actions = QtGui.QHBoxLayout()
self.Button_Run=QtGui.QPushButton('Run')
self.nbparam = QtGui.QTextEdit('3')
self.nbparam.setMaximumHeight(30)
self.nbparam.setMaximumWidth(50)
self.layout_Actions.addWidget(self.Button_Run)
self.layout_Actions.addWidget(self.nbparam)
self.horizontalGroupBox_Actions.setLayout(self.layout_Actions)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Actions)
self.centralWidget.setLayout(self.mainHBOX_param_scene)
self.updateWidget = None
# Param2 layout
self.horizontalGroupBox_Param2 = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
self.Param2_layout = QtGui.QVBoxLayout()
self.Button_Run.clicked.connect(self.UpdateLayout)
self.UpdateLayout()
# console
self.horizontalGroupBox_Console2 = QtGui.QGroupBox("Console ")
self.layout_Consol_Label2 = QtGui.QVBoxLayout()
self.Consol_Label2 = QtGui.QTextEdit('Console :')
self.layout_Consol_Label2.addWidget(self.Consol_Label2)
self.horizontalGroupBox_Console2.setLayout(self.layout_Consol_Label2)
self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Console2)
# parameter2
# From now on everything well be created inside a function that updates the layout
def UpdateLayout(self):
if (self.updateWidget == None):
self.updateWidget = QtGui.QWidget()
else:
self.mainHBOX_param_scene.removeWidget(self.updateWidget)
self.updateWidget.deleteLater()
self.updateWidget = None
cnt = self.mainHBOX_param_scene.layout().count()
print("Widget count:" +str(cnt)+ "")
nbparam = int(self.nbparam.toPlainText())
#self.horizontalGroupBox_Param2.setMaximumHeight(150)
grid2 = QtGui.QGridLayout()
self.Param2_Label = QtGui.QLabel('Name')
self.Param2 = QtGui.QComboBox()
self.Param2.addItems(["a","b","c"])
self.Param2.setMinimumWidth(100)
# self.Param2_layout.addWidget(self.Param2)
self.Param2_min_Label = QtGui.QLabel('min')
self.Param2_max_Label = QtGui.QLabel('max')
self.Param2_step_Label = QtGui.QLabel('step')
grid2.addWidget(self.Param2_Label, 0, 0)
grid2.addWidget(self.Param2_min_Label, 0, 1)
grid2.addWidget(self.Param2_max_Label, 0, 2)
grid2.addWidget(self.Param2_step_Label, 0, 3)
self.listofedit = []
text=["a","b","c"]
for i in np.arange(nbparam):
print(i)
self.listofedit.append([QtGui.QComboBox(),QtGui.QTextEdit('20'),QtGui.QTextEdit('50'),QtGui.QTextEdit('10')])
self.listofedit[i][0].addItems(text)
self.listofedit[i][0].setMinimumWidth(100)
self.listofedit[i][1].setMaximumWidth(40)
self.listofedit[i][1].setMaximumHeight(40)
self.listofedit[i][2].setMaximumWidth(40)
self.listofedit[i][2].setMaximumHeight(40)
self.listofedit[i][3].setMaximumWidth(40)
self.listofedit[i][3].setMaximumHeight(40)
grid2.addWidget(self.listofedit[i][0], i+1, 0)
grid2.addWidget(self.listofedit[i][1], i+1, 1)
grid2.addWidget(self.listofedit[i][2], i+1, 2)
grid2.addWidget(self.listofedit[i][3], i+1, 3)
self.updateWidget = QtGui.QWidget()
self.updateWidget.setLayout(grid2)
#self.updateWidget.setMaximumHeight(nbparam*60)
self.Param2_layout.addWidget(self.updateWidget,)
self.horizontalGroupBox_Param2.setLayout(self.Param2_layout)
#self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Param2)
# NEW
self.mainHBOX_param_scene.insertWidget(1,self.horizontalGroupBox_Param2)
return
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
对更新的回答
如果您愿意 updating QGroupBox 而不是更改位置,而不是 addWidget()
您需要的功能是 insertWidget()
函数。
我更新了 UpdateLayout
函数中添加小部件的行。希望适合你。
函数参考如下: