2

I'm currently building a pretty simple data plotting UI using pyqt, and I'm trying to segment my blocks of code for different parts of the UI into functions to help with code clarity and debugging. Because I'm initializing a number of instance attributes outside of init though I'm wondering what the best way to make sure I'm in compliance with PEP8.

Right now I'm handling this by setting everything equal to None in init and then continuing with my initializations where appropriate. I'm just wondering if there is a preferred / more Pythonic way to handle this.

I know this is similar to the discussion here: Instance attribute attribute_name defined outside __init__)

Was just wondering if there is a preferred solution since this seems like a much messier example.

Code:

 def __init__(self):
        QtWidgets.QWidget.__init__(self)

        #call to setup_ui to generate default window with associated widgets
        (self.windowLayout, self.leftColumn,
         self.treeWidget, self.tabWidget, self.line, self.plotWidget) = self.setup_ui(self)

        ##The following are a number of buttons and layouts all used to create the Image and Plot tab.
        ##See the "create_import_and_plot_tab" function for their various initializations
        (self.Import_and_Plot_tab, self.import_plot_layout, self.loadDataFolder_btn,
         self.clearChecked_btn, self.createPlot_btn, self.addToPlot_btn, self.plotNum_dropdown,
         self.clearPlot_dropdown, self.addPlotLayout, self.clearPlot_btn, self.clearPlotLayout) = (None, None, None,
                                                                                                   None, None, None,
                                                                                                   None, None, None,
                                                                                                   None, None)
        ##The following are a number of buttons and layouts all used to create the Markers tab.
        ##See the "create_markers_tab" function for their various initializations
        (self.Markers_tab, self.markers_tab_layout, self.addMarker_btn, self.markersPlot_dropdown) = (None, None,
                                                                                                      None, None)

def setup_ui(self, form):
        form.resize(1742, 988)
        form.setContextMenuPolicy(QtCore.Qt.PreventContextMenu)
        form.setWindowTitle("Data Viewer")
        pg.setConfigOption('background', 'w')
        pg.setConfigOption('foreground', 'k')

        self.windowLayout = QtWidgets.QHBoxLayout(form)
        self.windowLayout.setObjectName("windowLayout")

        self.leftColumn = QtWidgets.QVBoxLayout()
        self.leftColumn.setObjectName("leftColumnLayout")

        self.treeWidget = QtWidgets.QTreeWidget(form)
        self.treeWidget.setObjectName("treeWidget")
        self.treeWidget.headerItem().setText(0, "Data")

        self.tabWidget = QtWidgets.QTabWidget(form)
        tab_widget_size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        self.tabWidget.setSizePolicy(tab_widget_size_policy)
        self.tabWidget.setMinimumSize(QtCore.QSize(200, 100))
        self.tabWidget.setObjectName("tabWidget")

        self.tabWidget.addTab(self.create_import_and_plot_tab(form), "Import && Plot")
        self.tabWidget.addTab(self.create_markers_tab(form), "Markers")

        self.leftColumn.addWidget(self.treeWidget)
        self.leftColumn.addWidget(self.tabWidget)

        self.line = QtWidgets.QFrame(form)
        self.line.setFrameShape(QtWidgets.QFrame.VLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")

        self.plotWidget = pg.GraphicsLayoutWidget(form)
        self.plotWidget.setObjectName("plotWidget")

        self.windowLayout.addLayout(self.leftColumn)
        self.windowLayout.addWidget(self.line)
        self.windowLayout.addWidget(self.plotWidget, QtCore.Qt.AlignCenter)

        return self.windowLayout, self.leftColumn, self.treeWidget, self.tabWidget, self.line, self.plotWidget

def create_import_and_plot_tab(self, form):
        self.Import_and_Plot_tab = QtWidgets.QWidget()
        self.Import_and_Plot_tab.setObjectName("Import_and_Plot_tab")

        self.import_plot_layout = QtWidgets.QVBoxLayout(self.Import_and_Plot_tab)
        self.import_plot_layout.setObjectName("import_plot_layout")

        self.loadDataFolder_btn = QtWidgets.QPushButton("Load Data Folder", form)
        self.loadDataFolder_btn.setObjectName("loadDataFolder_btn")
        self.loadDataFolder_btn.clicked.connect(self.load_data_folder)
        self.loadDataFolder_btn.clicked.connect(self.update_treeWidget)

        self.clearChecked_btn = QtWidgets.QPushButton("Clear Check Boxes", form)
        self.clearChecked_btn.setObjectName("clearChecked_btn")
        self.clearChecked_btn.clicked.connect(self.clear_checked)

        self.createPlot_btn = QtWidgets.QPushButton("New Plot", form)
        self.createPlot_btn.setObjectName("createPlot_btn")
        self.createPlot_btn.clicked.connect(self.add_new_plot)

        self.addToPlot_btn = QtWidgets.QPushButton("Add plot to:", form)
        self.addToPlot_btn.setObjectName("addToPlot_btn_btn")
        self.addToPlot_btn.clicked.connect(self.add_to_plot)

        self.plotNum_dropdown = QtWidgets.QComboBox(form)
        self.plotNum_dropdown.setObjectName("plotNum_dropdown")

        self.clearPlot_dropdown = QtWidgets.QComboBox(form)
        self.clearPlot_dropdown.setObjectName("clearPlot_dropdown")
        self.clearPlot_dropdown.addItem("All")

        self.addPlotLayout = QtWidgets.QHBoxLayout()
        self.addPlotLayout.setObjectName("addPlotLayout")
        self.addPlotLayout.addWidget(self.addToPlot_btn)
        self.addPlotLayout.addWidget(self.plotNum_dropdown)

        self.clearPlot_btn = QtWidgets.QPushButton("Clear:", form)
        self.clearPlot_btn.setObjectName("clearPlot_btn")
        self.clearPlot_btn.clicked.connect(self.clear_plot)

        self.clearPlotLayout = QtWidgets.QHBoxLayout()
        self.clearPlotLayout.setObjectName("clearPlotLayout")
        self.clearPlotLayout.addWidget(self.clearPlot_btn)
        self.clearPlotLayout.addWidget(self.clearPlot_dropdown)

        self.import_plot_layout.addWidget(self.loadDataFolder_btn)
        self.import_plot_layout.addWidget(self.clearChecked_btn)
        self.import_plot_layout.addWidget(self.createPlot_btn)
        self.import_plot_layout.addLayout(self.addPlotLayout)
        self.import_plot_layout.addLayout(self.clearPlotLayout)

        return self.Import_and_Plot_tab

def create_markers_tab(self, form):
        self.Markers_tab = QtWidgets.QWidget()
        self.Markers_tab.setObjectName("Markers_tab")

        self.markers_tab_layout = QtWidgets.QVBoxLayout(self.Markers_tab)
        self.markers_tab_layout.setObjectName("markers_tab_layout")

        self.addMarker_btn = QtWidgets.QPushButton("Add Marker", form)
        self.addMarker_btn.setObjectName("addMarker_btn")
        self.addMarker_btn.clicked.connect(self.add_marker)

        self.markersPlot_dropdown = QtWidgets.QComboBox(form)
        self.markersPlot_dropdown.setObjectName("markersPlot_dropdown")
        self.markersPlot_dropdown.addItem("All")

        self.markers_tab_layout.addWidget(self.addMarker_btn)
        self.markers_tab_layout.addWidget(self.markersPlot_dropdown)

        return self.Markers_tab
3
  • Note that None is immutable, so you can do a = b = c = None instead of a, b, c = (None, None, None). This not only saves typing but it also avoids constructing a tuple unnecessarily. Commented Nov 26, 2014 at 15:30
  • 1
    @iCodez: CPython optimises the tuple away for the two or three-constant case, but it is still better to use a = b = c = None here. Commented Nov 26, 2014 at 15:33
  • Is there a way to string that across multiple lines? Commented Nov 26, 2014 at 15:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.