Tree control with check boxes

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Tree control with check boxes

 
0
  #1
Jun 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 46
Reputation: Fuse is an unknown quantity at this point 
Solved Threads: 3
Fuse's Avatar
Fuse Fuse is offline Offline
Light Poster

Re: Tree control with check boxes

 
0
  #2
Jun 6th, 2008
Care to elaborate... a lot? I mean it'd help if you mentioned what GUI module you're using, for a start.
Mir's Fuselage

Because what's not to like about being killed by a toilet seat from Mir's atmospheric re-entry?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Re: Tree control with check boxes

 
0
  #3
Jun 6th, 2008
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.
******************-************************-*******************************-*
  1. import wx
  2.  
  3. class MyFrame(wx.Frame):
  4. def __init__(self, parent, id, title):
  5. wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 350))
  6.  
  7. hbox = wx.BoxSizer(wx.HORIZONTAL)
  8. vbox = wx.BoxSizer(wx.VERTICAL)
  9. panel1 = wx.Panel(self, -1)
  10. panel2 = wx.Panel(self, -1)
  11.  
  12. self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1,-1), wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)
  13. root = self.tree.AddRoot('Programmer')
  14. os = self.tree.AppendItem(root, 'Operating Systems')
  15. pl = self.tree.AppendItem(root, 'Programming Languages')
  16. tk = self.tree.AppendItem(root, 'Toolkits')
  17. self.tree.AppendItem(os, 'Linux')
  18. self.tree.AppendItem(os, 'FreeBSD')
  19. self.tree.AppendItem(os, 'OpenBSD')
  20. self.tree.AppendItem(os, 'NetBSD')
  21. self.tree.AppendItem(os, 'Solaris')
  22. cl = self.tree.AppendItem(pl, 'Compiled languages')
  23. sl = self.tree.AppendItem(pl, 'Scripting languages')
  24. self.tree.AppendItem(cl, 'Java')
  25. self.tree.AppendItem(cl, 'C++')
  26. self.tree.AppendItem(cl, 'C')
  27. self.tree.AppendItem(cl, 'Pascal')
  28. self.tree.AppendItem(sl, 'Python')
  29. self.tree.AppendItem(sl, 'Ruby')
  30. self.tree.AppendItem(sl, 'Tcl')
  31. self.tree.AppendItem(sl, 'PHP')
  32. self.tree.AppendItem(tk, 'Qt')
  33. self.tree.AppendItem(tk, 'MFC')
  34. self.tree.AppendItem(tk, 'wxPython')
  35. self.tree.AppendItem(tk, 'GTK+')
  36. self.tree.AppendItem(tk, 'Swing')
  37. self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
  38. self.display = wx.StaticText(panel2, -1, '',(10,10), style=wx.ALIGN_CENTRE)
  39. vbox.Add(self.tree, 1, wx.EXPAND)
  40. hbox.Add(panel1, 1, wx.EXPAND)
  41. hbox.Add(panel2, 1, wx.EXPAND)
  42. panel1.SetSizer(vbox)
  43. self.SetSizer(hbox)
  44. self.Centre()
  45.  
  46. def OnSelChanged(self, event):
  47. item = event.GetItem()
  48. self.display.SetLabel(self.tree.GetItemText(item))
  49.  
  50. class MyApp(wx.App):
  51. def OnInit(self):
  52. frame = MyFrame(None, -1, 'treectrl.py')
  53. frame.Show(True)
  54. self.SetTopWindow(frame)
  55. return True
  56.  
  57. app = MyApp(0)
  58. app.MainLoop()


********************-**************************-*********************
Editors note: Please use the [code=python] and [/code] tag pair to enclose your python code.
Last edited by vegaseat; Jun 6th, 2008 at 1:53 pm. Reason: code tags
Attached Images
File Type: bmp treectrl.bmp (520.1 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 46
Reputation: Fuse is an unknown quantity at this point 
Solved Threads: 3
Fuse's Avatar
Fuse Fuse is offline Offline
Light Poster

Re: Tree control with check boxes

 
0
  #4
Jun 7th, 2008
Why not just create checkboxes, hide them, and then on the event of a tree node expansion, show the corresponding checkboxes?
Mir's Fuselage

Because what's not to like about being killed by a toilet seat from Mir's atmospheric re-entry?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,063
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is online now Online
Veteran Poster

Re: Tree control with check boxes

 
0
  #5
Jun 7th, 2008
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.
  1. import wx
  2.  
  3. class MyFrame(wx.Frame):
  4. def __init__(self, parent, id, title):
  5. wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 350))
  6.  
  7. hbox = wx.BoxSizer(wx.HORIZONTAL)
  8. vbox = wx.BoxSizer(wx.VERTICAL)
  9. panel1 = wx.Panel(self, -1)
  10. panel2 = wx.Panel(self, -1)
  11.  
  12. self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1,-1),
  13. wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)
  14. root = self.tree.AddRoot('Programmer')
  15. os = self.tree.AppendItem(root, 'Operating Systems')
  16. pl = self.tree.AppendItem(root, 'Programming Languages')
  17. tk = self.tree.AppendItem(root, 'Toolkits')
  18.  
  19. root_dict = {}
  20. root_dict[os] = ('Linux', 'FreeBSD', 'OpenBSD', 'NetBSD', 'Solaris')
  21. root_dict[tk] = ('Qt', 'MFC', 'wxPython', 'GTK+', 'Swing')
  22. for index in root_dict.keys():
  23. for item in root_dict[index]:
  24. self.tree.AppendItem(index, item)
  25.  
  26. cl = self.tree.AppendItem(pl, 'Compiled languages')
  27. sl = self.tree.AppendItem(pl, 'Scripting languages')
  28. pl_dict = {}
  29. pl_dict[cl] = ('Java', 'C++', 'C', 'Pascal')
  30. pl_dict[sl] = ('Python', 'Ruby', 'Tcl', 'PHP')
  31. for index in pl_dict.keys():
  32. for item in pl_dict[index]:
  33. self.tree.AppendItem(index, item)
  34.  
  35. self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
  36. self.display = wx.StaticText(panel2, -1, '',(10,10), style=wx.ALIGN_CENTRE)
  37. vbox.Add(self.tree, 1, wx.EXPAND)
  38. hbox.Add(panel1, 1, wx.EXPAND)
  39. hbox.Add(panel2, 1, wx.EXPAND)
  40. panel1.SetSizer(vbox)
  41. self.SetSizer(hbox)
  42. self.Centre()
  43.  
  44. def OnSelChanged(self, event):
  45. item = event.GetItem()
  46. self.display.SetLabel(self.tree.GetItemText(item))
  47.  
  48. class MyApp(wx.App):
  49. def OnInit(self):
  50. frame = MyFrame(None, -1, 'treectrl.py')
  51. frame.Show(True)
  52. self.SetTopWindow(frame)
  53. return True
  54.  
  55. app = MyApp(0)
  56. app.MainLoop()
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC