Python Forum
[PyQt] scrollbar in tab
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] scrollbar in tab
#1
I wanted to create a scrollbar(pyqt5) into tab, base on example in link below I did some changes to fit my need but the scrollbar didn't show on tab and my charts become smaller.

example https://stackoverflow.com/questions/4947...fixed-size

import file https://filebin.net/u3m7m3x2k74wlm6l
import sys
import os
import pandas as pd
#from pandas import DataFrame
from PyQt5.QtWidgets import ( QApplication, QLabel, QWidget,QHBoxLayout,QVBoxLayout,QPushButton,QTabWidget,QMainWindow,QGridLayout,QSizePolicy\
                              ,QDateEdit,QCalendarWidget,QLineEdit,QMessageBox,QFileDialog,QCompleter,QScrollArea,QWidget,QGroupBox)
#import sqlite3
from PyQt5.QtGui import *
from PyQt5.QtGui import QFont
from PyQt5.QtCore import *
# for graph
#import plotly.graph_objects as go 

#import html 
import plotly.offline as po
import plotly.express as px
import plotly.graph_objs as go
from PyQt5.QtWebEngineWidgets import *

class Win(QMainWindow):
    def __init__(self):
        super().__init__()    
        self.setGeometry(100,100, 1280,900)
        self.GuiApp=App()
        self.setCentralWidget(self.GuiApp)
        self.show()

class Tab2(QWidget):
    def __init__(self, parent=None):
        super(Tab2, self).__init__(parent)
        self.But1 = QPushButton ("   Test But  ", self)
        self.But1.show()


class Tab1(QWidget):
    def __init__(self, parent=None):
        super(Tab1, self).__init__(parent)

        df = pd.read_csv(r'C:/Users/User/Desktop/Python-setup test/Plot122.csv')
        data1 = px.line(df,x = 'Date', y ='AAPL.Open', color = 'CellName')
        fig4 = go.Figure(data1)

        raw_html = '<html><head><meta charset="utf-8" />'
        raw_html += '<script src="https://cdn.plot.ly/plotly-latest.min.js"></script></head>'
        raw_html += '<body>'
        raw_html += po.plot(fig4, include_plotlyjs=False, output_type='div')
        raw_html += '</body></html>'
#fig_view
        fig_view1 = QWebEngineView()
        fig_view2 = QWebEngineView()
        fig_view3 = QWebEngineView()
        fig_view4 = QWebEngineView()
        fig_view5 = QWebEngineView()
        fig_view6 = QWebEngineView()

        fig_view1.setHtml(raw_html)
        fig_view1.setFixedSize(700,400)
        fig_view1.show()

        fig_view2.setHtml(raw_html)
        fig_view2.setFixedSize(700,400)
        fig_view2.show()

        fig_view3.setHtml(raw_html)
        fig_view3.setFixedSize(700,400)
        fig_view3.show()

        fig_view4.setHtml(raw_html)
        fig_view4.setFixedSize(700,400)
        fig_view4.show()

        fig_view5.setHtml(raw_html)
        fig_view5.setFixedSize(700,400)
        fig_view5.show()

        fig_view6.setHtml(raw_html)
        fig_view6.setFixedSize(700,400)
        fig_view6.show()

        layoutC = QGridLayout()
        layoutC.addWidget(fig_view1,0,0,1,1)
        layoutC.addWidget(fig_view2,0,1,1,1)
        layoutC.addWidget(fig_view3,1,0,1,1)
        layoutC.addWidget(fig_view4,1,1,1,1)
        layoutC.addWidget(fig_view5,2,0,1,1)
        layoutC.addWidget(fig_view6,2,1,1,1)

        self.setLayout(layoutC)     
        self.setSizePolicy(QSizePolicy.Maximum,QSizePolicy.Maximum)


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.layout = QGridLayout(self)
        self.setLayout(self.layout)
        #Button
        BT1 = QPushButton('BT1',self) 
        BT2 = QPushButton('BT2',self) 
        BT1.setSizePolicy(QSizePolicy.Preferred,QSizePolicy.Fixed)
        BT2.setSizePolicy(QSizePolicy.Preferred,QSizePolicy.Fixed)
        #add Button into Widget with location
        self.layout.addWidget(BT1, 0,0,1,1) 
        self.layout.addWidget(BT2, 1,0,1,1)
#end
        ##########################################TEST Graogs ##################################################################################
        tab1 = Tab1(self)
        tab2 = Tab2(self)
        self.tabs = QTabWidget(self)     
        self.tabs.addTab(tab1, 'Page 1')
        self.tabs.addTab(tab2,'Page2')
        self.groupscrollbox = QGroupBox()
        self.MVB = QVBoxLayout() 
        self.MVB.addWidget(self.tabs) #add tab into QVBOX        
        
        widgetTab = QWidget(self) 
        widgetTab.setLayout(QHBoxLayout())
        widgetTab.layout().addWidget(self.groupscrollbox) #set groupbox as widget
        self.scroll = QScrollArea()
        self.scroll.setWidget(widgetTab)
        self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.scroll.setWidgetResizable(True)
        self.scroll.setEnabled(True)

        self.groupscrollbox.setLayout(self.MVB) 

        ###################################Test Graph@@#######################################################################################      
        self.layout.addWidget(self.groupscrollbox, 0,2,13,1) 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Win()
    sys.exit(app.exec_())       
Reply
#2
Not sure, but I think setting the size policy to fixed may be messing you up. Fixed would be the wrong choice for the widget that contains all your charts. You want the charts to be fixed size and the chart container to resize to fit the charts. However I think the documentation says size policy for a widget is ignored if the widget has a layout manager for it's children, so it may not make any difference what you specify for size policy.

If you want help, you should provide an example that other people can run. If I had this problem the first thing I would do is see if I could reproduce this problem without having to have a data file and maybe not requiring pandas and plotly. Maybe I could reproduce the problem by replacing the charts with large, fixed size buttons. If so, I would have code that anyone programming in Qt could test (even PySide2 users). If not, I would have more insight into my problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Help create scrollbar in chatbot with tkinter on python fenec10 4 1,432 Aug-07-2023, 02:59 PM
Last Post: deanhystad
  [Tkinter] Scrollbar apffal 7 3,061 Oct-11-2021, 08:26 PM
Last Post: deanhystad
Question [Tkinter] How to configure scrollbar dimension? water 6 3,374 Jan-03-2021, 06:16 PM
Last Post: deanhystad
  [Tkinter] Scrollbar in tkinter PatrickNoir 2 3,263 Jul-26-2020, 06:02 PM
Last Post: deanhystad
  [Tkinter] Help with Scrollbar JJota 6 3,587 Mar-10-2020, 05:25 AM
Last Post: Larz60+
  [Tkinter] Scrollbar doesn't work on Canvas in Tkinter DeanAseraf1 3 9,305 Sep-19-2019, 03:26 PM
Last Post: joe_momma
  [Tkinter] Same Scrollbar for two text area smabubakkar 3 2,810 Jun-19-2019, 05:26 PM
Last Post: Denni
  Scrollbar rturus 5 15,036 Jun-06-2019, 01:04 PM
Last Post: heiner55
  [PyGUI] Create a scrollbar in GUI to add test cases mamta_parida 1 3,585 Sep-27-2018, 11:57 AM
Last Post: Larz60+
  [Tkinter] Scrollbar problem & general organization weatherman 13 13,325 Apr-16-2017, 12:55 PM
Last Post: weatherman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020