Dadep Dadep - 1 year ago 166
Python Question

PyQt open a subwindow with table

I would like to create a window with a button "ruban" and when I click on this button it open a new window with a table containing the "data".
My problem is when i run it, and i click on the button it doesn't appear anything....
Thanks a lot for any help !

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

data = {'col1':['1','2','3'], 'col2':['4','5','6'], 'col3':['7','8','9']}

class Window (QtGui.QMainWindow):

def __init__(self):
super(Window, self).__init__()
self.setGeometry(100, 100, 500, 300)
self.setWindowTitle("Machine de Turing")


def home(self):

btn1 = QtGui.QPushButton("Ruban", self)

def edit_ruban(self):

table = MyTable(self, data, 5, 3)

class MyTable(QTableWidget):
def __init__(self, data, *args):
QTableWidget.__init__(self, *args) = data

def setmydata(self):

horHeaders = []
for n, key in enumerate(sorted(
for m, item in enumerate([key]):
newitem = QTableWidgetItem(item)
self.setItem(m, n, newitem)

def run():
app = QtGui.QApplication(sys.argv)


Answer Source

You have the wrong arguments in call to MyTable.__init__.
It's not clear why you add self as the first arg. Python provides implicit self so it's data that must go first.
If you intended to provide parent window for MyTable it must go last, see the signature QTableWidget::QTableWidget ( int rows, int columns, QWidget * parent = 0 ),
so the call would loo like this:

table = MyTable(data, 5, 3, self)

But MyTable by default will be emdedded into Window Apparently that's not you want. To avoid embedding either omit parent argument and save a reference to the newly created table

def edit_ruban(self):
  table = MyTable(data, 5, 3) # no parent provided
  self.table = table # this prevents the garbage collector
                     # from deleting the new table

or better set window flags to Qt.Window

def edit_ruban(self):
  table = MyTable(data, 5, 3, self)
  table.setWindowFlags(table.windowFlags() | Qt.Window)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download