1
我目前正在製作一個GUI,我想要一些滑塊,一些按鈕和一些圖。我正在努力將Matplotlib圖放在我想要的位置。情節是在一個QVBoxLayout中,我試圖把它放在一個Widget中而沒有成功。我希望能夠選擇情節Python -PyQt Matplotlib繪圖定位
這裏的位置和大小是我現在有:
這裏是我想要的東西,在這裏我可以定義位置和大小,所以我對其他控件空間:
What I am looking for, where I can define position and size
這是基本的代碼:
import sys
import numpy as np
from PyQt4 import QtGui, QtCore
# import inspect
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
#PLOTTING
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, self)
self.plot()
# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
self.setLayout(layout)
#WINDOW PROPERTIES
self.resize(800,800)
self.setWindowTitle('Waveguide Array')
self.setWindowIcon(QtGui.QIcon('flavicon.png'))
self.show()
def plot(self):
''' plot some random stuff '''
data = [np.random.random() for i in range(10)]
ax = self.figure.add_subplot(111)
ax.hold(False)
ax.plot(data, '*-')
self.canvas.draw()
# def update_plot(self):
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
感謝您的幫助!
爲什麼不使用QGridLayout作爲佈局,將畫布添加到小部件,並將小部件添加到GridLayout?請記住設置widget的最大尺寸()大約是視圖尺寸的四分之一,或者添加其他小部件,並確保所有比例成比例。 –