我将一些小部件放在网格中。但是一直以来,这些小部件的行为都很奇怪。
如果在附近放置ImageView
和PlotWidget
,则ImageView
弯曲和PlotWidged
会拉伸。我放了两个ImageView
和两个PlotWidget
进行检查-一切正常。怎么了?
import sys
import numpy as np
from PyQt5.QtWidgets import QWidget, QGridLayout, QPushButton, QApplication
import pyqtgraph as pg
from PIL import Image
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)
self.Imv = pg.ImageView()
win = pg.PlotWidget()
win.plot(np.random.random_sample(40)*10)
btn1 = QPushButton("Start")
btn2 = QPushButton('Forward/backward')
btn3 = QPushButton('Filters')
grid.addWidget(self.Imv, 0, 0, 1, 1)
grid.addWidget(win, 0, 1, 1, 1)
grid.addWidget(btn1, 1, 0, 1, 1)
grid.addWidget(btn2, 2, 0, 1, 1)
grid.addWidget(btn3, 3, 0, 1, 1)
btn1.clicked.connect(self.cap)
self.move(300, 150)
self.setWindowTitle('Scentific camera view')
self.show()
def cap(self):
global Imv
img = np.array(Image.open('D:/Frames/image7.jpg'))
self.Imv.setImage(img.T)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
所以看起来:
附近有两个
ImageView
-很好:最佳答案
我建议使用QTDesigner构建您的UI并自动生成代码。这使事情变得容易得多。如果必须手动操作,请尝试设置尺寸策略以扩展或将几何更改为固定尺寸:
win.setGeometry(QtCore.QRect(10, 20, 621, 181))
win.setSizePolicy(QtWidgets.QSizePolicy.Expanding)
关于python - 为什么PlotWidget无法在GridLayout单元中缩放?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59303718/