[英]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
你好
我需要从1toN数据库构建一个treectrl。 但是只运行一次迭代。 我的意思是控件中只有一个父项和其中的子项。 它为什么不建立其他父项,即使它们存在于数据库中呢?
提前致谢。
解决了:
我为“ noktalar”添加了另一个光标。
您确定gruplar和noktalar变量是可迭代的吗? 添加一些行以打印这些行,并确保您要遍历某些内容。 否则,代码对我来说似乎正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.