Python Program Expecting a Return
<p>I'm having trouble getting this program I'm writing to execute correctly. I'm trying to build a table, within a separate class. The method within the class seems to require a return, in order for the table to show and I'm trying to figure out why. If I use "return" or "return None" the table will not show when I run the program. However, if I use a nonsense reference like "return junk", it will compile with the error that the reference is not defined but the table will appear when I run the program. Can someone help we with this? </p>

<pre><code>from PyQt4.QtCore import *
from PyQt4.QtGui  import *
import sys

class MyMainWindow(QMainWindow):

    def __init__(self, parent=None):

        super(MyMainWindow, self).__init__(parent)
        self.table_widget = TableWidget(self)
        self.setCentralWidget(self.table_widget)

class TableWidget(QWidget):

    def __init__(self, parent):
        super(TableWidget, self).__init__(parent)

        table = QTableWidget()
        tableItem = QTableWidgetItem()

        # initiate table
        table.setWindowTitle("test_table")
        table.resize(400, 250)
        table.setRowCount(4)
        table.setColumnCount(2)

        # set data
        table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
        table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
        table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
        table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
        table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
        table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
        table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
        table.setItem(3,1, QTableWidgetItem("Item (4,2)"))
        table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
        table.setItem(3,1, QTableWidgetItem("Item (4,2)"))

        # show table
        table.show()
        return junk

def main():
    app = QApplication(sys.argv)
    GUI = MyMainWindow()
    GUI.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
</code></pre>

<p>You're not keeping a reference to the table, so it gets garbage-collected when <code>__init__</code> returns. You can fix this by putting the table in a layout, like this:</p>

<pre><code>class TableWidget(QWidget):

    def __init__(self, parent):
        super(TableWidget, self).__init__(parent)

        table = QTableWidget()
        tableItem = QTableWidgetItem()

        table.setRowCount(4)
        table.setColumnCount(2)

        # set data
        table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
        table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
        table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
        table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
        table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
        table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
        table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
        table.setItem(3,1, QTableWidgetItem("Item (4,2)"))
        table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
        table.setItem(3,1, QTableWidgetItem("Item (4,2)"))

        layout = QVBoxLayout(self)
        layout.addWidget(table)
</code></pre>

python python-3.x pyqt4