简体   繁体   中英

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_DEFAULT_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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM