更好/更多Pythonic方式来处理init外部定义的实例属性?

时间:2014-11-26 15:28:24

标签: python constructor pep8

我目前正在使用pyqt构建一个非常简单的数据绘图UI,我正在尝试将用于UI不同部分的代码块分段为函数,以帮助实现代码清晰度和调试。因为我正在初始化 init 之外的一些实例属性,虽然我想知道什么是确保我符合PEP8的最佳方法。

现在我通过在 init 中将所有内容设置为None来处理此问题,然后在适当的时候继续我的初始化。我只是想知道是否有一种首选/更多的Pythonic方法来处理这个问题。

我知道这与此处的讨论类似:Instance attribute attribute_name defined outside __init__

只是想知道是否有一个首选的解决方案,因为这似乎是一个更加混乱的例子。

代码:

 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

0 个答案:

没有答案