Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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_Python 3.x_Pyqt_Pyqt5_Qwidget - Fatal编程技术网

Python 使小部件尽可能大

Python 使小部件尽可能大,python,python-3.x,pyqt,pyqt5,qwidget,Python,Python 3.x,Pyqt,Pyqt5,Qwidget,以下是我编写(和/或改编自其他来源)的一些代码: 它生成以下窗口作为其输出: 我希望中间的情节占据尽可能多的空间,这样红色的背景就变得不可见了。 如果我排除设置窗口大小的命令,就会发生这种情况,事实上就是这样。然而,窗户太小了,我需要把它弄大 我尝试过使用self.view.setGeometry,但似乎没有什么不同。我已经查看了中的可用模块,但不知道哪一个可能有用。如果要确定显示的绘图覆盖了窗口中的所有可用空间,我认为没有必要使用QGraphicsView,因为Fig是一个QWidget,可

以下是我编写(和/或改编自其他来源)的一些代码:

它生成以下窗口作为其输出:

我希望中间的情节占据尽可能多的空间,这样红色的背景就变得不可见了。 如果我排除设置窗口大小的命令,就会发生这种情况,事实上就是这样。然而,窗户太小了,我需要把它弄大


我尝试过使用
self.view.setGeometry
,但似乎没有什么不同。我已经查看了中的可用模块,但不知道哪一个可能有用。

如果要确定显示的绘图覆盖了窗口中的所有可用空间,我认为没有必要使用
QGraphicsView
,因为
Fig
是一个
QWidget
,可以直接在
setCentralWidget()中使用

更新:使用QScrollArea
如果要确定显示的绘图覆盖了窗口内的所有可用空间,我认为无需使用
QGraphicsView
,因为
Fig
是一个
QWidget
,可以直接在
setCentralWidget()中使用

更新:使用QScrollArea
是的,但使用QGraphicsView的目的是,当窗口缩小时,它会免费提供滚动条。@Lupacante好的,如果您想使用滚动条,我将更新我的解决方案。:D@Lupacante检查我的答案完美,我真的很感谢你帮助我,谢谢!是的,但使用QGraphicsView的目的是,当窗口缩小时,它会免费提供滚动条。@Lupacante好的,如果您想使用滚动条,我将更新我的解决方案。:D@Lupacante检查我的答案完美,我真的很感谢你帮助我,谢谢!
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import (QGraphicsView, QGraphicsScene, QMainWindow,
                             QApplication, QWidget, QVBoxLayout, 
                             QDesktopWidget)
from PyQt5.QtGui import QBrush
from PyQt5.QtCore import Qt
import sys

class View(QGraphicsView):

    def __init__(self):
        super(View, self).__init__()

        self.initScene()

    def initScene(self):     

        self.scene = QGraphicsScene()
        self.canvas = Fig()
        self.setBackgroundBrush(QBrush(Qt.red))
        self.canvas.draw()
        self.setScene(self.scene)
        self.scene.addWidget(self.canvas)

class Fig(FigureCanvas):

    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')


class Window(QMainWindow):

    def __init__(self):

        QMainWindow.__init__(self)

        desktop = QDesktopWidget()
        rect = desktop.availableGeometry()
        self.setGeometry(rect.width()/10, rect.height()/10, rect.width()/1.2,
                   rect.height()/1.2)

        self.view = View()
        self.setCentralWidget(self.view)

app = QApplication(sys.argv)
window = Window()
window.show()
app.exec_()
class Fig(FigureCanvas):
    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')


class Window(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        desktop = QDesktopWidget()
        rect = desktop.availableGeometry()
        self.setGeometry(rect.width()/10, rect.height()/10, rect.width()/1.2,
                   rect.height()/1.2)

        self.canvas = Fig()
        self.canvas.draw()
        self.setCentralWidget(self.canvas)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
class Fig(FigureCanvas):
    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')

    def showEvent(self, event):
        self.setFixedSize(self.size())
        FigureCanvas.showEvent(self, event)


class Window(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        desktop = QDesktopWidget()
        rect = desktop.availableGeometry()
        self.setGeometry(rect.width()/10, rect.height()/10, rect.width()/1.2,
                   rect.height()/1.2)

        self.canvas = Fig()
        self.canvas.draw()

        scrollArea = QScrollArea()
        scrollArea.setWidgetResizable(True)
        scrollArea.setWidget(self.canvas)
        self.setCentralWidget(scrollArea)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())