gpt4 book ai didi

python - PyQT5中的GridLayout和VerticalLayout

转载 作者:行者123 更新时间:2023-12-01 01:26:59 32 4
gpt4 key购买 nike

我是Python2.7中的PyQT5新手。我喜欢具有两个组件的 Verticle 布局。第一个区域用于显示,第二个区域用于控制小部件。

现在的问题是(1)控制小部件的间距不均匀,(2)第一个区域比第二个区域需要更多的空间。

我怎样才能做到这一点?

我的代码如下。

class Thread(QThread):
changePixmap = pyqtSignal(QImage)
def run(self):
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if ret:
rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
convertToQtFormat = QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0], QImage.Format_RGB888)
p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.changePixmap.emit(p)


class PlayStreaming(QWidget):
def __init__(self):
super(PlayStreaming,self).__init__()
self.initUI()

@pyqtSlot(QImage)
def setImage(self, image):
self.label.setPixmap(QPixmap.fromImage(image))

def initUI(self):
self.setWindowTitle("Image")
self.setGeometry(100, 100, 600, 400)
self.resize(1800, 1200)
# create a label
self.label = QLabel(self)
self.label.move(280, 120)
self.label.resize(640, 480)
th = Thread(self)
th.changePixmap.connect(self.setImage)
th.start()



class UIWidget(QWidget):



def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)

# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.tabs.resize(800,600)

# Add tabs
self.tabs.addTab(self.tab1,"Face")
self.tabs.addTab(self.tab2,"Human")
self.tabs.addTab(self.tab3,"Vehicle")

# Create first tab
self.createGridLayout()
self.tab1.layout = QVBoxLayout(self)
self.display=PlayStreaming()
self.tab1.layout.addWidget(self.display)
self.tab1.layout.addWidget(self.horizontalGroupBox)
self.tab1.setLayout(self.tab1.layout)

# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
def createGridLayout(self):
self.horizontalGroupBox = QGroupBox("Control")
layout = QGridLayout()
layout.setColumnStretch(2, 3)
layout.addWidget(QPushButton('Test'),0,0)
layout.addWidget(QPushButton('Run'),0,1)
layout.addWidget(QPushButton('Set Faces'),0,2)
layout.addWidget(QPushButton('Recognize'),1,0)
layout.addWidget(QPushButton('Rescale'),1,1)
layout.addWidget(QPushButton('FacePose'),1,2)
self.horizontalGroupBox.setLayout(layout)

当前状态为 enter image description here

我怎样才能做到这一点?

最佳答案

你必须使用strech来设置权重,默认情况下每个widget的stretch都是0,所以你只需要将self.display的stretch设置为1即可。另一方面,没有必要使用 setColumnStretch(),因为默认情况下所有按钮都具有相同的大小:

from PyQt5 import QtCore, QtGui, QtWidgets 
import cv2

class Thread(QtCore.QThread):
changePixmap = QtCore.pyqtSignal(QtGui.QImage)
def run(self):
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if ret:
rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
convertToQtFormat = QtGui.QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0], QtGui.QImage.Format_RGB888)
p = convertToQtFormat.scaled(640, 480, QtCore.Qt.KeepAspectRatio)
self.changePixmap.emit(p)


class PlayStreaming(QtWidgets.QWidget):
def __init__(self):
super(PlayStreaming,self).__init__()
self.initUI()

@QtCore.pyqtSlot(QtGui.QImage)
def setImage(self, image):
self.label.setPixmap(QtGui.QPixmap.fromImage(image))

def initUI(self):
self.setWindowTitle("Image")
# create a label
self.label = QtWidgets.QLabel(self)
th = Thread(self)
th.changePixmap.connect(self.setImage)
th.start()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)


class UIWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(UIWidget, self).__init__(parent)
# Initialize tab screen
self.tabs = QtWidgets.QTabWidget()
self.tab1 = QtWidgets.QWidget()
self.tab2 = QtWidgets.QWidget()
self.tab3 = QtWidgets.QWidget()

# Add tabs
self.tabs.addTab(self.tab1,"Face")
self.tabs.addTab(self.tab2,"Human")
self.tabs.addTab(self.tab3,"Vehicle")

# Create first tab
self.createGridLayout()
self.tab1.layout = QtWidgets.QVBoxLayout()
self.display = PlayStreaming()
self.tab1.layout.addWidget(self.display, stretch=1)
self.tab1.layout.addWidget(self.horizontalGroupBox)
self.tab1.setLayout(self.tab1.layout)

# Add tabs to widget
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.tabs)

def createGridLayout(self):
self.horizontalGroupBox = QtWidgets.QGroupBox("Control")
layout = QtWidgets.QGridLayout()
layout.addWidget(QtWidgets.QPushButton('Test'),0,0)
layout.addWidget(QtWidgets.QPushButton('Run'),0,1)
layout.addWidget(QtWidgets.QPushButton('Set Faces'),0,2)
layout.addWidget(QtWidgets.QPushButton('Recognize'),1,0)
layout.addWidget(QtWidgets.QPushButton('Rescale'),1,1)
layout.addWidget(QtWidgets.QPushButton('FacePose'),1,2)
self.horizontalGroupBox.setLayout(layout)


if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = UIWidget()
w.show()
sys.exit(app.exec_())

关于python - PyQT5中的GridLayout和VerticalLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53249623/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com