Cant iterate database for building treectrl in WxPython
import wx
import sqlite3 as lite
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 350))
vbox = wx.BoxSizer(wx.VERTICAL)
self.tree = wx.TreeCtrl( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TR_DEFAU开发者_高级运维LT_STYLE|wx.TR_HIDE_ROOT )
self.root = self.tree.AddRoot('Noktalar')
vbox.Add(self.tree, 1, wx.EXPAND)
con = lite.connect('noktalar.sdb')
#cur = con.cursor() #removed
cur2 = con.cursor() #added
gruplar=cur.execute("select * from gruplar")
for grup in gruplar:
parentItem = self.tree.AppendItem(self.root, grup[1])
deyim="""select * from noktalar where noktalar.grup_id="""+str(grup[0])
#noktalar=cur.execute(deyim) #removed
noktalar=cur2.execute(deyim) #added
for nokta in noktalar:
self.tree.AppendItem(parentItem, nokta[1])
con.commit()
cur.close()
cur2.close() #added
con.close()
self.SetSizer(vbox)
self.Centre()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'treectrl.py')
frame.Show(True)
self.SetTopWindow(frame)
return True
Hi,
I need to build a treectrl from a 1toN database. But just one iteration is run. I mean there is only one parent item in the control and subitems inside it. Why doesnt it build the other parent items even if they exist in database?
Thanks in advance.
SOLVED:
I added another cursor for "noktalar".
Are you sure that the gruplar and noktalar variables are iterable? Add some lines to print those and make sure you're iterating over something. Otherwise, the code looks right to me.
精彩评论