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><code>__init__</code> as constructor can't return value. </p>

<hr>

<p>Every <code>Widget</code> needs a parent - so <code>QTableWidget</code> needs too.</p>

<pre><code>    table = QTableWidget(parent)
</code></pre>

<p>Because you send <code>MainWindow</code> to your class as parent so now <code>MainWindow</code> is <code>table</code> parent and <code>MainWindow</code> can show <code>table</code> in its area.</p>

<hr>

<p><code>table</code> is not (main) window so it can't set <code>WindowTitle</code>. You have to do it in <code>MainWindow</code> (or use <code>parent</code> in <code>TableWidget</code>). </p>

<p>The same with <code>resize()</code> - to resize window you have to use it in <code>MainWindow</code> (or use <code>parent</code> in <code>TableWidget</code>). </p>

<p><code>show()</code> is use to show (main) window. </p>

<hr>

<p>You could define your class using <code>QTableWidget</code> </p>

<pre><code> class TableWidget(QTableWidget):
</code></pre>

<p>and then you can use <code>self</code> instead of <code>table</code> </p>

<hr>

<p><strong>EDIT:</strong> full code</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)

        #self.setWindowTitle("test_table")
        #self.resize(400, 250)

        self.show()


class TableWidget(QTableWidget):

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

        # change main window
        parent.setWindowTitle("test_table")
        parent.resize(400, 250)

        # initiate table
        self.setRowCount(4)
        self.setColumnCount(2)

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


def main():
    app = QApplication(sys.argv)
    win = MyMainWindow() # probably it has to be assigned to variable
    #win.show() # use if you don't have `self.show()` in class
    sys.exit(app.exec_())

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

python python-3.x pyqt4