Trying to fill two lists fails

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Trying to fill two lists fails

Jordy Deweer
Dear group
I am totally new here, so sorry if I don't do things the right way. Please tell me if I do so.
I am trying to fil a wx.ListBox and a wx.ListCtrl with contents, but it seems to fail.
The code for the listbox is as follows:
def makeMainWindow(self):
# A sizer which places the list of tables and the table structure next to eachother
box = wx.BoxSizer(wx.HORIZONTAL)
# List of tables
listpart = wx.BoxSizer(wx.VERTICAL)
listLabel = wx.StaticText(self.window, label = _('Available tables'))
self.tables = [' ']
self.FillList()
self.tablelist = wx.ListBox(self.window, choices=self.tables, style=wx.LB_SINGLE|wx.LB_NEEDED_SB)
self.Bind(wx.EVT_LISTBOX, self.FillTable, self.tablelist)
listpart.Add(listLabel,0)
listpart.Add(self.tablelist,0)
box.Add(listpart,0)
# add the table view
tablepart = wx.BoxSizer(wx.VERTICAL)
tblLabel = wx.StaticText(self.window, label = _('Table view'))
self.table = wx.ListCtrl(self.window, 0, style = wx.LC_REPORT)
tablepart.Add(tblLabel,0)
tablepart.Add(self.table,0)
box.Add(tablepart,0)
self.SetSizer(box)
This works as expected, but filling the list and the table doesn't work. The code is below:

# Fill the data with needed columns and rows
def FillTable(self, e):
# Set columns
for colName in dbops.GetColumns(self.dbPath, self.tablelist.GetString(self.tablelist.GetSelection())):
self.table.AppendColumn(colName)
for row in dbops.AllRows(self.dbPath, self.tablelist.GetString(self.tablelist.GetSelection())):
self.table.Append(row)
def FillList(self):
if self.dbPath != None:
tablelst = dbops.tableList(self.dbPath)
if tablelst:
self.tables.extend(tablelst)

The code from dbops is below again: 
def tableList(path):
    try:
        if path != None:
            # connect to opened database
            db = sqlite3.connect(path)
            # create a cursor object to explore the database
            c = db.cursor()
            c.execute("""SELECT name FROM sqlite_master WHERE type='table';""")
            # get all records from the previous SQL query
            all_records = c.fetchall()
            # create the list for the returning
            tablelist = []
            for row in all_records:
                tablelist.extend(row)
            c.close()
            db.close()
            return tablelist
        else:
            return False
    except(e):
        print(e)

def GetColumns(dbPath, table):
    # Check if database is opened
    if dbPath != None:
        columns = []
        # Create connection with database
        db = sqlite3.connect(dbPath)
        cursor = db.cursor()
        # Execute query
        cursor.execute("PRAGMA table_info('%s');" %(table))
        rows = cursor.fetchall()
        for colName in rows:
            columns.append(colName[1])
        cursor.close()
        db.close()
        return columns

def AllRows(dbPath, table):
    data = []
    
    if dbPath != None:
        db = sqlite3.connect(dbPath)
        cursor = db.cursor()
        cursor.execute("""SELECT * FROM %s""" % (table))
        rows = cursor.fetchall()
        for r in rows:
            data.append(r)
        cursor.close()
        db.close()
        return data

When I run the program, I get the following error:
Traceback (most recent call last):
  File "C:\Users\jordy\eclipse-workspace\sqlexplo_lite\src\sqlexplo_lite.py", line 165, in FillTable
    for colName in dbops.GetColumns(self.dbPath, self.tablelist.GetString(self.tablelist.GetSelection())):
wx._core.wxAssertionError: C++ assertion "IsValid(n)" failed at ..\..\src\msw\listbox.cpp(394) in wxListBox::GetString(): invalid index in wxListBox::GetString

In fact, I understand the above error, but I don't see where it goes wrong. Is there someone here who can help me find a solution by pointing out something maybe, or giving em some tips?

Thanks in advance to anyone who can give me a small hint.

Regards, Jordy.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Trying to fill two lists fails

Tim Roberts
Jordy Deweer wrote:

I am totally new here, so sorry if I don't do things the right way. Please tell me if I do so.
I am trying to fil a wx.ListBox and a wx.ListCtrl with contents, but it seems to fail.
The code for the listbox is as follows:
...
This works as expected, but filling the list and the table doesn't work. The code is below:

# Fill the data with needed columns and rows
def FillTable(self, e):
# Set columns
for colName in dbops.GetColumns(self.dbPath, self.tablelist.GetString(self.tablelist.GetSelection())):
self.table.AppendColumn(colName)
for row in dbops.AllRows(self.dbPath, self.tablelist.GetString(self.tablelist.GetSelection())):
self.table.Append(row)
...
When I run the program, I get the following error:
Traceback (most recent call last):
  File "C:\Users\jordy\eclipse-workspace\sqlexplo_lite\src\sqlexplo_lite.py", line 165, in FillTable
    for colName in dbops.GetColumns(self.dbPath, self.tablelist.GetString(self.tablelist.GetSelection())):
wx._core.wxAssertionError: C++ assertion "IsValid(n)" failed at ..\..\src\msw\listbox.cpp(394) in wxListBox::GetString(): invalid index in wxListBox::GetString

In fact, I understand the above error, but I don't see where it goes wrong. Is there someone here who can help me find a solution by pointing out something maybe, or giving em some tips?

In all of that code, I don't see that you ever put anything into self.tablelist.  Thus, when you call FillTable, self.tablelist.GetSelection() returns -1, meaning nothing is selected.  Are you, perhaps, missing code in FillList that should have populaterd self.tablelist from the tablelst?
-- 
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Trying to fill two lists fails

Jordy Deweer

Hi there

The FillList function add the columns to the self.tables and in 'def makeMainWindow()', you see that self.tablelist's choices argument uses the self.tables to fill.

Thanks in advance.

Vriendelijke groeten, Kind regards
Jordy Deweer

On 9/11/2018 6:16 PM, Tim Roberts wrote:
Jordy Deweer wrote:

I am totally new here, so sorry if I don't do things the right way. Please tell me if I do so.
I am trying to fil a wx.ListBox and a wx.ListCtrl with contents, but it seems to fail.
The code for the listbox is as follows:
...
This works as expected, but filling the list and the table doesn't work. The code is below:

# Fill the data with needed columns and rows
def FillTable(self, e):
# Set columns
for colName in dbops.GetColumns(self.dbPath, self.tablelist.GetString(self.tablelist.GetSelection())):
self.table.AppendColumn(colName)
for row in dbops.AllRows(self.dbPath, self.tablelist.GetString(self.tablelist.GetSelection())):
self.table.Append(row)
...
When I run the program, I get the following error:
Traceback (most recent call last):
  File "C:\Users\jordy\eclipse-workspace\sqlexplo_lite\src\sqlexplo_lite.py", line 165, in FillTable
    for colName in dbops.GetColumns(self.dbPath, self.tablelist.GetString(self.tablelist.GetSelection())):
wx._core.wxAssertionError: C++ assertion "IsValid(n)" failed at ..\..\src\msw\listbox.cpp(394) in wxListBox::GetString(): invalid index in wxListBox::GetString

In fact, I understand the above error, but I don't see where it goes wrong. Is there someone here who can help me find a solution by pointing out something maybe, or giving em some tips?

In all of that code, I don't see that you ever put anything into self.tablelist.  Thus, when you call FillTable, self.tablelist.GetSelection() returns -1, meaning nothing is selected.  Are you, perhaps, missing code in FillList that should have populaterd self.tablelist from the tablelst?
-- 
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Trying to fill two lists fails

Tim Roberts
Jordy Deweer wrote:
>
> The FillList function add the columns to the self.tables and in 'def
> makeMainWindow()', you see that self.tablelist's choices argument uses
> the self.tables to fill.
>

You are right, my apologies.  However, I don't see that you ever select
a default item.  If you print self.tablelist.GetSelection(), I presume
you'll see that it is -1.

--
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.


--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

smime.p7s (5K) Download Attachment