I am trying to add finplot
, https://pypi.org/project/finplot/, as a widget to one of the layouts in my UI. I created a widget for finplot
and added it to the widgets in the layout but I get the following error:
self.tab1.layout.addWidget(self.tab1.fplt_widget) TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'PlotItem'
Here is the code that I used.
import sys from PyQt5.QtWidgets import * import finplot as fplt import yfinance # Creating the main window class App(QMainWindow): def __init__(self): super().__init__() self.title = 'PyQt5 - QTabWidget' self.left = 0 self.top = 0 self.width = 600 self.height = 400 self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.tab_widget = MyTabWidget(self) self.setCentralWidget(self.tab_widget) self.show() # Creating tab widgets class MyTabWidget(QWidget): def __init__(self, parent): super(QWidget, self).__init__(parent) self.layout = QVBoxLayout(self) # Initialize tab screen self.tabs = QTabWidget() self.tabs.setTabPosition(QTabWidget.East) self.tabs.setMovable(True) self.tab1 = QWidget() self.tabs.resize(600, 400) # Add tabs self.tabs.addTab(self.tab1, "tab1") self.tab1.layout = QVBoxLayout(self) self.tab1.label = QLabel("AAPL") self.tab1.df = yfinance.download('AAPL') self.tab1.fplt_widget = fplt.create_plot_widget(self.window()) fplt.candlestick_ochl(self.tab1.df[['Open', 'Close', 'High', 'Low']]) self.tab1.layout.addWidget(self.tab1.label) self.tab1.layout.addWidget(self.tab1.fplt_widget) self.tab1.setLayout(self.tab1.layout) # Add tabs to widget self.layout.addWidget(self.tabs) self.setLayout(self.layout) if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_())
Advertisement
Answer
The create_plot_widget()
function creates a PlotItem
that cannot be added to a layout, the solution is to use some QWidget that can display the content of the PlotItem
as the PlotWidget
:
import pyqtgraph as pg # ... self.tab1.df = yfinance.download("AAPL") self.tab1.fplt_widget = pg.PlotWidget( plotItem=fplt.create_plot_widget(self.window()) ) fplt.candlestick_ochl(self.tab1.df[["Open", "Close", "High", "Low"]])