How to create a tree control with child nodes having check boxes? I want the check boxes to be preferably on the left side of the nodes.
dinilkarun 8 Posting Whiz in Training
Fuse 17 Light Poster
Care to elaborate... a lot? I mean it'd help if you mentioned what GUI module you're using, for a start.
dinilkarun 8 Posting Whiz in Training
We are using this code.We are unable to append check box corresponding to each node.
For example, I need check boxes for node named LINUX,FreeBSD etc and hencelikewise.
please see attachment.
******************-************************-*******************************-*
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 350))
hbox = wx.BoxSizer(wx.HORIZONTAL)
vbox = wx.BoxSizer(wx.VERTICAL)
panel1 = wx.Panel(self, -1)
panel2 = wx.Panel(self, -1)
self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1,-1), wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)
root = self.tree.AddRoot('Programmer')
os = self.tree.AppendItem(root, 'Operating Systems')
pl = self.tree.AppendItem(root, 'Programming Languages')
tk = self.tree.AppendItem(root, 'Toolkits')
self.tree.AppendItem(os, 'Linux')
self.tree.AppendItem(os, 'FreeBSD')
self.tree.AppendItem(os, 'OpenBSD')
self.tree.AppendItem(os, 'NetBSD')
self.tree.AppendItem(os, 'Solaris')
cl = self.tree.AppendItem(pl, 'Compiled languages')
sl = self.tree.AppendItem(pl, 'Scripting languages')
self.tree.AppendItem(cl, 'Java')
self.tree.AppendItem(cl, 'C++')
self.tree.AppendItem(cl, 'C')
self.tree.AppendItem(cl, 'Pascal')
self.tree.AppendItem(sl, 'Python')
self.tree.AppendItem(sl, 'Ruby')
self.tree.AppendItem(sl, 'Tcl')
self.tree.AppendItem(sl, 'PHP')
self.tree.AppendItem(tk, 'Qt')
self.tree.AppendItem(tk, 'MFC')
self.tree.AppendItem(tk, 'wxPython')
self.tree.AppendItem(tk, 'GTK+')
self.tree.AppendItem(tk, 'Swing')
self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
self.display = wx.StaticText(panel2, -1, '',(10,10), style=wx.ALIGN_CENTRE)
vbox.Add(self.tree, 1, wx.EXPAND)
hbox.Add(panel1, 1, wx.EXPAND)
hbox.Add(panel2, 1, wx.EXPAND)
panel1.SetSizer(vbox)
self.SetSizer(hbox)
self.Centre()
def OnSelChanged(self, event):
item = event.GetItem()
self.display.SetLabel(self.tree.GetItemText(item))
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'treectrl.py')
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()
********************-**************************-*********************
Editors note: Please use the [code=python] and [/code] tag pair to enclose your python code.
This attachment is potentially unsafe to open. It may be an executable that is capable of making changes to your file system, or it may require specific software to open. Use caution and only open this attachment if you are comfortable working with x-ms-bmp files.
Fuse 17 Light Poster
Why not just create checkboxes, hide them, and then on the event of a tree node expansion, show the corresponding checkboxes?
woooee 814 Nearly a Posting Maven
Although I don't use wxPython, you should be able to use a dictionary to clean up the code. Something like this. Again I don't use this toolkit so it may require tuning.
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 350))
hbox = wx.BoxSizer(wx.HORIZONTAL)
vbox = wx.BoxSizer(wx.VERTICAL)
panel1 = wx.Panel(self, -1)
panel2 = wx.Panel(self, -1)
self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1,-1),
wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)
root = self.tree.AddRoot('Programmer')
os = self.tree.AppendItem(root, 'Operating Systems')
pl = self.tree.AppendItem(root, 'Programming Languages')
tk = self.tree.AppendItem(root, 'Toolkits')
root_dict = {}
root_dict[os] = ('Linux', 'FreeBSD', 'OpenBSD', 'NetBSD', 'Solaris')
root_dict[tk] = ('Qt', 'MFC', 'wxPython', 'GTK+', 'Swing')
for index in root_dict.keys():
for item in root_dict[index]:
self.tree.AppendItem(index, item)
cl = self.tree.AppendItem(pl, 'Compiled languages')
sl = self.tree.AppendItem(pl, 'Scripting languages')
pl_dict = {}
pl_dict[cl] = ('Java', 'C++', 'C', 'Pascal')
pl_dict[sl] = ('Python', 'Ruby', 'Tcl', 'PHP')
for index in pl_dict.keys():
for item in pl_dict[index]:
self.tree.AppendItem(index, item)
self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
self.display = wx.StaticText(panel2, -1, '',(10,10), style=wx.ALIGN_CENTRE)
vbox.Add(self.tree, 1, wx.EXPAND)
hbox.Add(panel1, 1, wx.EXPAND)
hbox.Add(panel2, 1, wx.EXPAND)
panel1.SetSizer(vbox)
self.SetSizer(hbox)
self.Centre()
def OnSelChanged(self, event):
item = event.GetItem()
self.display.SetLabel(self.tree.GetItemText(item))
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'treectrl.py')
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.