How to grow or shrink the MainWinodw based on content Widgets in PyQt5?

51 Views Asked by At

My problem: How to grow or shrink the main window size based on its dependent widget? Here is my code. In My code, I use StackLayout, group box, Labels, Pushbutton, and QLineedit. If I change the Item in the first combo box from "Excel" to "CSV", the item will change and display the contents. But In that Place, My necessity is to change the main window size. That is the main window will become shrink to its content. If I select "CSV" to "Excel", then Mainwindow will grow and show all contents.

import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QFileDialog,QPushButton, QLabel, QVBoxLayout,
                      QHBoxLayout,QLineEdit,QSizePolicy,QComboBox,QStyle,QStyleFactory,QGroupBox,QStackedWidget,
                      QFrame)
from PyQt5.QtGui import QFont,QIcon
from PyQt5.QtCore import QDir,Qt,QRect,QPoint

combo_box_data_source_type_item = ["Excel/","CSV/","JSON/","XML/"]
combo_box_excel_available_sheets_item = ["All"]
combo_box_excel_available_Columns_item = ["All"]

class MyStackedlayout(QStackedWidget):
    def minimumSize(self):
        if self.currentWidget():
            s = self.currentWidget().minimumSize()
            if s.isEmpty():
                s = self.currentWidget().minimumSizeHint()
                return s
            return super().minimumSize()

class DialogApp(QWidget):
   def __init__(self):
      super().__init__()

      self.style_window = QStyleFactory.create("Windows")
      self.current_data_source_type_index = 0
      self.current_data_source_type_text = ""


      self.overall_frame = QFrame()
      self.overall_frame_layout = QVBoxLayout(self.overall_frame)

      self.left_layout = QVBoxLayout()
      self.main_layout = QHBoxLayout()
      self.main_frame = QFrame()
      self.main_frame_layout = QVBoxLayout()
      self.overall_frame_layout.addLayout(self.left_layout)
      self.main_layout.addWidget(self.overall_frame)
      self.setLayout(self.main_layout)
      self.setLayout(self.main_frame_layout)

      self.lbl_data_source_type = QLabel("Data Source Type")
      self.lbl_data_source_type.adjustSize()
      self.lbl_input_file = QLabel("Input File")
      self.lbl_input_file.adjustSize()

      self.combobox_data_source_type = QComboBox()
      self.combobox_data_source_type.setFixedSize(420,40)
      self.combobox_data_source_type.setFont(QFont("Lucida Sans Unicode",11 ))
      self.combobox_data_source_type.addItems(combo_box_data_source_type_item)
      self.combobox_data_source_type.setCurrentIndex(0)
      self.current_data_source_type_index = 0
      self.current_data_source_type_text = self.combobox_data_source_type.currentText()
      self.combobox_data_source_type.currentIndexChanged.connect(self.selectionchange_data_source_type)

      self.textbox_file_path = QLineEdit()
      self.textbox_file_path.setFixedSize(376,40)
      self.textbox_file_path.setReadOnly(True)

      self.button_open_path = QPushButton()
      self.button_open_path.setObjectName("ob_btn_openpath")
      self.button_open_path.setFocusPolicy(Qt.NoFocus)
      self.button_open_path.setFixedSize(40,40)
      self.button_open_path.setIcon((QIcon(self.style_window.standardIcon(QStyle.SP_DialogOpenButton))))
      self.button_open_path.clicked.connect(self.get_text_file)

      self.input_file_path_hbox= QHBoxLayout()
      self.input_file_path_hbox.addWidget(self.textbox_file_path)
      self.input_file_path_hbox.addWidget(self.button_open_path)
      self.input_file_path_hbox.setSpacing(4)
      self.input_file_path_hbox.setContentsMargins(0,0,0,0)
      self.input_file_path_hbox.setAlignment(Qt.AlignLeft)

      self.groupbox_data_souce_options = QGroupBox("Data Source Options")
      self.groupbox_data_souce_options.setFixedWidth(420)

      self.lbl_available_sheets = QLabel("Available Sheets")
      self.lbl_available_sheets.adjustSize()
      self.lbl_available_columns = QLabel("Available Columns")
      self.lbl_available_columns.adjustSize()
      self.combobox_available_sheets = QComboBox()
      self.combobox_available_sheets.setFixedSize(400,40)
      self.combobox_available_sheets.setFont(QFont("Lucida Sans Unicode", 11))
      self.combobox_available_sheets.setCurrentIndex(0)

      self.groupbox_excel_layout = QVBoxLayout()
      self.groupbox_data_souce_options.setLayout(self.groupbox_excel_layout)
      self.groupbox_excel_layout.addWidget(self.lbl_available_sheets)
      self.groupbox_excel_layout.addWidget(self.combobox_available_sheets)
      self.groupbox_excel_layout.addWidget(self.lbl_available_columns)
      self.groupbox_excel_layout.addStretch()

      self.dialog = QFileDialog()
      self.dialog.setWindowTitle("Pick File to Import")
      self.dialog.setViewMode(QFileDialog.ViewMode.Detail)

      self.stackwidget = MyStackedlayout()

      self.left_layout.addStretch()
      self.left_layout.addWidget(self.lbl_data_source_type)
      self.left_layout.addWidget(self.combobox_data_source_type)
      self.left_layout.addWidget(self.lbl_input_file)
      self.left_layout.addLayout(self.input_file_path_hbox)
      self.left_layout.setAlignment(Qt.AlignLeft)
      self.left_layout.addWidget(self.stackwidget)
      self.stackwidget.addWidget(self.groupbox_data_souce_options)
      self.left_layout.addStretch()


   def selectionchange_data_source_type(self,index):
      self.current_data_source_type_index = index
      self.current_data_source_type_text = self.combobox_data_source_type.currentText()
      if self.current_data_source_type_text.split('/')[0] == "Excel":
         self.stackwidget.setCurrentWidget(self.groupbox_data_souce_options)
      elif self.current_data_source_type_text.split('/')[0] == "CSV":
         lbl1 = QLabel("Csv Group Box")
         self.stackwidget.addWidget(lbl1)
         self.stackwidget.setCurrentWidget(lbl1)

      stackwidget_resize = MyStackedlayout.minimumSize(self.stackwidget)
      # print(stackwidget_resize)
      self.stackwidget.resize(stackwidget_resize)

   def get_text_file(self):

      if self.current_data_source_type_text.split('/')[0] == "Excel":
         self.dialog.setNameFilters(["Excel File (*.xlsx *.xls)"])
      elif self.current_data_source_type_text.split('/')[0] == "CSV":
         self.dialog.setNameFilters(["CSV File (*.csv)"])

      self.dialog.setFileMode(QFileDialog.ExistingFiles)
      self.dialog.fileSelected.connect(self.file_selected)

if __name__ == '__main__':
   app = QApplication(sys.argv)
   demo = DialogApp()
   demo.show()
   sys.exit(app.exec_())
0

There are 0 best solutions below