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:
JavaScript
x
3
1
self.tab1.layout.addWidget(self.tab1.fplt_widget)
2
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'PlotItem'
3
Here is the code that I used.
JavaScript
1
63
63
1
import sys
2
from PyQt5.QtWidgets import *
3
import finplot as fplt
4
import yfinance
5
6
7
# Creating the main window
8
class App(QMainWindow):
9
def __init__(self):
10
super().__init__()
11
self.title = 'PyQt5 - QTabWidget'
12
self.left = 0
13
self.top = 0
14
self.width = 600
15
self.height = 400
16
self.setWindowTitle(self.title)
17
self.setGeometry(self.left, self.top, self.width, self.height)
18
19
self.tab_widget = MyTabWidget(self)
20
self.setCentralWidget(self.tab_widget)
21
22
self.show()
23
24
# Creating tab widgets
25
26
27
class MyTabWidget(QWidget):
28
def __init__(self, parent):
29
super(QWidget, self).__init__(parent)
30
self.layout = QVBoxLayout(self)
31
32
# Initialize tab screen
33
self.tabs = QTabWidget()
34
self.tabs.setTabPosition(QTabWidget.East)
35
self.tabs.setMovable(True)
36
37
self.tab1 = QWidget()
38
39
self.tabs.resize(600, 400)
40
41
# Add tabs
42
self.tabs.addTab(self.tab1, "tab1")
43
44
45
self.tab1.layout = QVBoxLayout(self)
46
self.tab1.label = QLabel("AAPL")
47
self.tab1.df = yfinance.download('AAPL')
48
self.tab1.fplt_widget = fplt.create_plot_widget(self.window())
49
fplt.candlestick_ochl(self.tab1.df[['Open', 'Close', 'High', 'Low']])
50
self.tab1.layout.addWidget(self.tab1.label)
51
self.tab1.layout.addWidget(self.tab1.fplt_widget)
52
self.tab1.setLayout(self.tab1.layout)
53
54
# Add tabs to widget
55
self.layout.addWidget(self.tabs)
56
self.setLayout(self.layout)
57
58
59
if __name__ == '__main__':
60
app = QApplication(sys.argv)
61
ex = App()
62
sys.exit(app.exec_())
63
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
:
JavaScript
1
9
1
import pyqtgraph as pg
2
3
# ...
4
5
self.tab1.df = yfinance.download("AAPL")
6
self.tab1.fplt_widget = pg.PlotWidget(
7
plotItem=fplt.create_plot_widget(self.window())
8
)
9
fplt.candlestick_ochl(self.tab1.df[["Open", "Close", "High", "Low"]])