简体   繁体   English

无法在WxPython中迭代数据库以构建treectrl

[英]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. 我需要从1toN数据库构建一个treectrl。 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". 我为“ noktalar”添加了另一个光标。

Are you sure that the gruplar and noktalar variables are iterable? 您确定gruplar和noktalar变量是可迭代的吗? Add some lines to print those and make sure you're iterating over something. 添加一些行以打印这些行,并确保您要遍历某些内容。 Otherwise, the code looks right to me. 否则,代码对我来说似乎正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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