2011-02-24 4 views
15

में चेकबॉक्स के साथ एक वृक्ष दृश्य कैसे बनाएं I छोटे कार्यक्रम को लिखने के लिए टिंकर और टिक्स का उपयोग कर रहे हैं। मैं एक बिंदु पर हूं जहां मुझे चेकबॉक्स (चेकबूटन) के साथ एक पेड़ दृश्य की आवश्यकता है ताकि मैं पेड़ दृश्य से आइटम का चयन कर सकूं। क्या ऐसा करने का कोई आसान तरीका है? मैं ttk.Treeview() देख रहा हूं और पेड़ दृश्य प्राप्त करना आसान लगता है लेकिन क्या दृश्य में चेकबटन डालने का कोई तरीका है?पायथन

एक साधारण कोड स्निपेट की वास्तव में सराहना की जाएगी।

मैं ttk तक सीमित नहीं हूं। कुछ भी करेगा; जब तक मैं एक उदाहरण या अच्छा डॉक्स है मैं यह काम

उत्तर

17

enter image description here

import Tix 

class View(object): 
    def __init__(self, root): 
     self.root = root 
     self.makeCheckList() 

    def makeCheckList(self): 
     self.cl = Tix.CheckList(self.root, browsecmd=self.selectItem) 
     self.cl.pack() 
     self.cl.hlist.add("CL1", text="checklist1") 
     self.cl.hlist.add("CL1.Item1", text="subitem1") 
     self.cl.hlist.add("CL2", text="checklist2") 
     self.cl.hlist.add("CL2.Item1", text="subitem1") 
     self.cl.setstatus("CL2", "on") 
     self.cl.setstatus("CL2.Item1", "on") 
     self.cl.setstatus("CL1", "off") 
     self.cl.setstatus("CL1.Item1", "off") 
     self.cl.autosetmode() 

    def selectItem(self, item): 
     print item, self.cl.getstatus(item) 

def main(): 
    root = Tix.Tk() 
    view = View(root) 
    root.update() 
    root.mainloop() 

if __name__ == '__main__': 
    main() 
+2

क्या चेकबॉक्स को देशी दिखने के लिए किसी भी तरह से ttk शामिल करना संभव है? – pihentagy

+3

क्या टिक्स और टिंकटर का उपयोग किए बिना इसे बनाने का कोई तरीका है? –

+0

मेरे पास पाइथन 2.7 है और मेरे पास टिक्स इंस्टॉल नहीं है, इसलिए मैं एक विकल्प खोजने की कोशिश कर रहा हूं। –

5

मैं ttk.Treeview इनहेरिट चेक बॉक्स के साथ एक treeview वर्ग बना कर सकते हैं, लेकिन चेक बॉक्स ttk.Checkbutton लेकिन की छवियों नहीं हैं चेक, अनचेक और ट्रिस्टेट चेकबॉक्स।

import tkinter as tk 
import tkinter.ttk as ttk 

class CheckboxTreeview(ttk.Treeview): 
    """ 
     Treeview widget with checkboxes left of each item. 
     The checkboxes are done via the image attribute of the item, so to keep 
     the checkbox, you cannot add an image to the item. 
    """ 

    def __init__(self, master=None, **kw): 
     ttk.Treeview.__init__(self, master, **kw) 
     # checkboxes are implemented with pictures 
     self.im_checked = tk.PhotoImage(file='checked.png') 
     self.im_unchecked = tk.PhotoImage(file='unchecked.png') 
     self.im_tristate = tk.PhotoImage(file='tristate.png') 
     self.tag_configure("unchecked", image=self.im_unchecked) 
     self.tag_configure("tristate", image=self.im_tristate) 
     self.tag_configure("checked", image=self.im_checked) 
     # check/uncheck boxes on click 
     self.bind("<Button-1>", self.box_click, True) 

    def insert(self, parent, index, iid=None, **kw): 
     """ same method as for standard treeview but add the tag 'unchecked' 
      automatically if no tag among ('checked', 'unchecked', 'tristate') 
      is given """ 
     if not "tags" in kw: 
      kw["tags"] = ("unchecked",) 
     elif not ("unchecked" in kw["tags"] or "checked" in kw["tags"] 
        or "tristate" in kw["tags"]): 
      kw["tags"] = ("unchecked",) 
     ttk.Treeview.insert(self, parent, index, iid, **kw) 

    def check_descendant(self, item): 
     """ check the boxes of item's descendants """ 
     children = self.get_children(item) 
     for iid in children: 
      self.item(iid, tags=("checked",)) 
      self.check_descendant(iid) 

    def check_ancestor(self, item): 
     """ check the box of item and change the state of the boxes of item's 
      ancestors accordingly """ 
     self.item(item, tags=("checked",)) 
     parent = self.parent(item) 
     if parent: 
      children = self.get_children(parent) 
      b = ["checked" in self.item(c, "tags") for c in children] 
      if False in b: 
       # at least one box is not checked and item's box is checked 
       self.tristate_parent(parent) 
      else: 
       # all boxes of the children are checked 
       self.check_ancestor(parent) 

    def tristate_parent(self, item): 
     """ put the box of item in tristate and change the state of the boxes of 
      item's ancestors accordingly """ 
     self.item(item, tags=("tristate",)) 
     parent = self.parent(item) 
     if parent: 
      self.tristate_parent(parent) 

    def uncheck_descendant(self, item): 
     """ uncheck the boxes of item's descendant """ 
     children = self.get_children(item) 
     for iid in children: 
      self.item(iid, tags=("unchecked",)) 
      self.uncheck_descendant(iid) 

    def uncheck_ancestor(self, item): 
     """ uncheck the box of item and change the state of the boxes of item's 
      ancestors accordingly """ 
     self.item(item, tags=("unchecked",)) 
     parent = self.parent(item) 
     if parent: 
      children = self.get_children(parent) 
      b = ["unchecked" in self.item(c, "tags") for c in children] 
      if False in b: 
       # at least one box is checked and item's box is unchecked 
       self.tristate_parent(parent) 
      else: 
       # no box is checked 
       self.uncheck_ancestor(parent) 

    def box_click(self, event): 
     """ check or uncheck box when clicked """ 
     x, y, widget = event.x, event.y, event.widget 
     elem = widget.identify("element", x, y) 
     if "image" in elem: 
      # a box was clicked 
      item = self.identify_row(y) 
      tags = self.item(item, "tags") 
      if ("unchecked" in tags) or ("tristate" in tags): 
       self.check_ancestor(item) 
       self.check_descendant(item) 
      else: 
       self.uncheck_descendant(item) 
       self.uncheck_ancestor(item) 



if __name__ == '__main__': 
    root = tk.Tk() 
    t = CheckboxTreeview(root, show="tree") 
    t.pack() 
    t.insert("", 0, "1", text="1") 
    t.insert("1", "end", "11", text="1") 
    t.insert("1", "end", "12", text="2") 
    t.insert("12", "end", "121", text="1") 
    t.insert("12", "end", "122", text="2") 
    t.insert("122", "end", "1221", text="1") 
    t.insert("1", "end", "13", text="3") 
    t.insert("13", "end", "131", text="1") 
    root.mainloop() 

CheckboxTreeview का एक उन्नत संस्करण ttkwidgets मॉड्यूल में उपलब्ध है।

+0

विचार अच्छा है लेकिन मुझे यकीन नहीं है कि यह 'वृक्षदृश्य' में कुछ निश्चित प्रविष्टियों के बाद कितना अच्छा है। यहां तक ​​कि यदि छवि छोटी है, फिर भी यह स्मृति के उस छोटे हिस्से को नहीं लेता है। – rbaleksandar

+0

@rbaleksandar मैं आपका बिंदु देखता हूं, यह बड़े पेड़ के लिए उपयोग करने योग्य नहीं है, लेकिन यदि आपके पास सैकड़ों आइटम नहीं हैं, तो यह टिक्स के मुकाबले एक अच्छा पेड़ बनाता है। –

+0

यह एक स्केल करने योग्य समाधान क्यों नहीं है? क्या छवि हर ट्रीनोड में कॉपी की गई है? मैं उम्मीद करता हूं कि छवि डेटा का केवल संदर्भ नोड्स के साथ साझा किया जाएगा। तो स्मृति में केवल 2 छवियां हैं। या मैं गलत हूँ? – Hatatister