2021年3月9日星期二

How to split PyQt5 MainDialog functions into separate files

I have a PyQT5 project that I am currently working on, and I have been neglecting splitting it up into several files. Below, I have included a MainDialog class which connects the GUI structure (made in QtDesigner) to several functions (e.g. open_dialog_box_command). How can I move these functions (e.g. open_dialog_box_command) into separate module files? Any help is much appreciated.

# -*- coding: utf-8 -*-  import os  from pathlib import Path  import sys  from pathvalidate import is_valid_filename  import pandas as pd  import matplotlib.pyplot as plt  import time  from PyQt5 import QtCore, QtWidgets, QtGui  from PyQt5.QtWidgets import QFileDialog  import ASMADA_GUI_structure  import ASMADA_read_file  import ASMADA_pre_processing  import ASMADA_analyze_data  import ASMADA_make_plots  import ASMADA_export_files      # Save relevant directory locations. This is necessary to correctly save   # output files in the desired folder.  path_to_python_code = Path.cwd()  path_to_parent_dir = path_to_python_code  #path_to_parent_dir = path_to_python_code.parent  path_to_Exported_Files_folder = path_to_parent_dir / "Exported_Files"  os.chdir(path_to_parent_dir)        # MainDialog manages all functions of Graphical User Interface (GUI).  class MainDialog(QtWidgets.QMainWindow,                    QtGui.QFont,                    ASMADA_GUI_structure.Ui_MainWindow):        def __init__(self, parent=None):          """          Creates connection between buttons/spinboxes/textboxes/etc. to the          functions within the MainDialog class.          """                    super(MainDialog, self).__init__(parent)          self.setupUi(self)            # Connect the 'open file' button and the command that          # actually captures the selected file's filepath.          self.open_file_button.clicked.connect(self.open_dialog_box_command)          self.skip_rows.valueChanged.connect(self.preview_data_command)          self.temp_col.valueChanged.connect(self.preview_data_command)          self.strain_col.valueChanged.connect(self.preview_data_command)          self.temp_unit.currentIndexChanged.connect(self.preview_data_command)          self.strain_unit.currentIndexChanged.connect(self.preview_data_command)          # Adjust the appearance of the dropdown menu for strain units (this           # saves space on the GUI).          combo_view = QtWidgets.QListView()          combo_view.setFixedWidth(81)                  self.strain_unit.setView(combo_view)          font = QtGui.QFont('MS Shell Dlg 2', 10)          self.strain_unit.setFont(font)          self.cycles_to_analyze.currentIndexChanged.connect(self.enable_custom_cycle_entry_command)          self.reset_inputs.clicked.connect(self.reset_inputs_command)          self.run_analysis.clicked.connect(self.run_analysis_command)          self.stop_analysis.clicked.connect(self.terminate_worker_command)          self.stop_analysis.setEnabled(False)            app.aboutToQuit.connect(self.terminate_worker_command)                      def open_dialog_box_command(self):          """          Allows user to go use the file directory to choose a file for analysis.          """            self.reset_inputs_command()            # Allow only .txt or .csv files to be selected.          filt = "Text or CSV files (*.txt *.csv)"          file_dlg_data = QFileDialog.getOpenFileName(None,                                                      "Select file for analysis",                                                      str(path_to_parent_dir),                                                      filter = filt)            # Create global variable "filepath" for user's selected file.          global filepath          filepath = Path(file_dlg_data[0])                    if filepath != '':              file_name = filepath.name              # Display filename on GUI.              self.display_file_name_label.setText(file_name)              # Load a preview of the file to the preview table.              self.preview_data_command()          def preview_data_command(self):          ...          def enable_custom_cycle_entry_command(self, index):          ...          def run_analysis_command(self):          ...          def lock_inputs_command(self, bool_var):          ...          def update_progress_command(self, text):          ...          def error_message_command(self, text):          ...                      def reset_error_messages_command(self):          ...          def reset_inputs_command(self):          ...          def terminate_worker_command(self):          ...            # WorkerThread manages all aspects of analysis.  class WorkerThread(QtCore.QThread,                      ASMADA_read_file.read_file_class,                      ASMADA_pre_processing.pre_processing_class,                      ASMADA_analyze_data.analyze_data_class,                     ASMADA_make_plots.make_plots_class,                     ASMADA_export_files.export_files_class):        notifyProgress = QtCore.pyqtSignal(object)      notifyError = QtCore.pyqtSignal(object)          def __init__(self, myvar, parent=None):                    super(WorkerThread, self).__init__(parent)          self.myvar = myvar          def run(self):          ...                          def stop(self):          self.terminate()      if __name__ == "__main__":      app = QtWidgets.QApplication(sys.argv)      form = MainDialog()      form.show()      sys.exit(app.exec_())  
https://stackoverflow.com/questions/66557781/how-to-split-pyqt5-maindialog-functions-into-separate-files March 10, 2021 at 11:07AM

没有评论:

发表评论