Blame platform-demos/ko/treeview_cellrenderertoggle.py.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="treeview_cellrenderertoggle.py" xml:lang="ko">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">TreeView와 TreeStore(Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#treeview"/>
Packit 1470ea
    <link type="next" xref="widget_drawing.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-06-30" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Marta Maria Casetti</name>
Packit 1470ea
      <email its:translate="no">mmcasetti@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>TreeStore를 보여주는 TreeView(CellRendererToggle이 들어간 더 복잡한 예제)</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>조성호</mal:name>
Packit 1470ea
      <mal:email>shcho@gnome.org</mal:email>
Packit 1470ea
      <mal:years>2017</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>좀 더 복잡한 TreeView와 TreeStore</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/treeview_cellrenderertoggle.png"/>
Packit 1470ea
  

이 TreeView에서는 두 열을 TreeStore로 나타내는데, 두 열 중 하나는 상태 반전 방식으로 보여줍니다.

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
    <title>예제 결과를 만드는 코드</title>
Packit 1470ea
Packit 1470ea
    from gi.repository import Gtk
Packit 1470ea
from gi.repository import Pango
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
books = [["Tolstoy, Leo", ["War and Peace", True], ["Anna Karenina", False]],
Packit 1470ea
         ["Shakespeare, William", ["Hamlet", False],
Packit 1470ea
             ["Macbeth", True], ["Othello", False]],
Packit 1470ea
         ["Tolkien, J.R.R.", ["The Lord of the Rings", False]]]
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="Library", application=app)
Packit 1470ea
        self.set_default_size(250, 100)
Packit 1470ea
        self.set_border_width(10)
Packit 1470ea
Packit 1470ea
        # the data are stored in the model
Packit 1470ea
        # create a treestore with two columns
Packit 1470ea
        self.store = Gtk.TreeStore(str, bool)
Packit 1470ea
        # fill in the model
Packit 1470ea
        for i in range(len(books)):
Packit 1470ea
            # the iter piter is returned when appending the author in the first column
Packit 1470ea
            # and False in the second
Packit 1470ea
            piter = self.store.append(None, [books[i][0], False])
Packit 1470ea
            # append the books and the associated boolean value as children of
Packit 1470ea
            # the author
Packit 1470ea
            j = 1
Packit 1470ea
            while j < len(books[i]):
Packit 1470ea
                self.store.append(piter, books[i][j])
Packit 1470ea
                j += 1
Packit 1470ea
Packit 1470ea
        # the treeview shows the model
Packit 1470ea
        # create a treeview on the model self.store
Packit 1470ea
        view = Gtk.TreeView()
Packit 1470ea
        view.set_model(self.store)
Packit 1470ea
Packit 1470ea
        # the cellrenderer for the first column - text
Packit 1470ea
        renderer_books = Gtk.CellRendererText()
Packit 1470ea
        # the first column is created
Packit 1470ea
        column_books = Gtk.TreeViewColumn("Books", renderer_books, text=0)
Packit 1470ea
        # and it is appended to the treeview
Packit 1470ea
        view.append_column(column_books)
Packit 1470ea
Packit 1470ea
        # the cellrenderer for the second column - boolean rendered as a toggle
Packit 1470ea
        renderer_in_out = Gtk.CellRendererToggle()
Packit 1470ea
        # the second column is created
Packit 1470ea
        column_in_out = Gtk.TreeViewColumn("Out?", renderer_in_out, active=1)
Packit 1470ea
        # and it is appended to the treeview
Packit 1470ea
        view.append_column(column_in_out)
Packit 1470ea
        # connect the cellrenderertoggle with a callback function
Packit 1470ea
        renderer_in_out.connect("toggled", self.on_toggled)
Packit 1470ea
Packit 1470ea
        # add the treeview to the window
Packit 1470ea
        self.add(view)
Packit 1470ea
Packit 1470ea
    # callback function for the signal emitted by the cellrenderertoggle
Packit 1470ea
    def on_toggled(self, widget, path):
Packit 1470ea
        # the boolean value of the selected row
Packit 1470ea
        current_value = self.store[path][1]
Packit 1470ea
        # change the boolean value of the selected row in the model
Packit 1470ea
        self.store[path][1] = not current_value
Packit 1470ea
        # new current value!
Packit 1470ea
        current_value = not current_value
Packit 1470ea
        # if length of the path is 1 (that is, if we are selecting an author)
Packit 1470ea
        if len(path) == 1:
Packit 1470ea
            # get the iter associated with the path
Packit 1470ea
            piter = self.store.get_iter(path)
Packit 1470ea
            # get the iter associated with its first child
Packit 1470ea
            citer = self.store.iter_children(piter)
Packit 1470ea
            # while there are children, change the state of their boolean value
Packit 1470ea
            # to the value of the author
Packit 1470ea
            while citer is not None:
Packit 1470ea
                self.store[citer][1] = current_value
Packit 1470ea
                citer = self.store.iter_next(citer)
Packit 1470ea
        # if the length of the path is not 1 (that is, if we are selecting a
Packit 1470ea
        # book)
Packit 1470ea
        elif len(path) != 1:
Packit 1470ea
            # get the first child of the parent of the book (the first book of
Packit 1470ea
            # the author)
Packit 1470ea
            citer = self.store.get_iter(path)
Packit 1470ea
            piter = self.store.iter_parent(citer)
Packit 1470ea
            citer = self.store.iter_children(piter)
Packit 1470ea
            # check if all the children are selected
Packit 1470ea
            all_selected = True
Packit 1470ea
            while citer is not None:
Packit 1470ea
                if self.store[citer][1] == False:
Packit 1470ea
                    all_selected = False
Packit 1470ea
                    break
Packit 1470ea
                citer = self.store.iter_next(citer)
Packit 1470ea
            # if they do, the author as well is selected; otherwise it is not
Packit 1470ea
            self.store[piter][1] = all_selected
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        win.show_all()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="methods">
Packit 1470ea
    <title>TreeView 위젯에 쓸만한 메서드</title>
Packit 1470ea
    

TreeView 위젯은 Model/View/Controller 방식으로 설계했습니다. Model은 데이터를, View는 바뀐 내용 알림을 받은 후의 모델의 내용을, 마지막으로 Controller는 모델의 상태를 바꾸고 뷰에게 바뀐 모델의 상태를 알리는 코드를 담습니다. 자세한 내용과 TreeModel에 대한 쓸만한 메서드 목록을 보시려면 <link xref="model-view-controller.py"/>를 참고하십시오.

Packit 1470ea
    

48번째 줄에서 "toggled" 시그널은 widget.connect(signal, callback function) 함수로 on_toggled() 콜백 함수에 연결했습니다. 더 자세한 설명은 <link xref="signals-callbacks.py"/>를 참조하십시오.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
    <title>API 참고서</title>
Packit 1470ea
    

이 예제는 다음 참고자료가 필요합니다:

Packit 1470ea
    <list>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkTreeView.html">GtkTreeView</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkTreeModel.html">GtkTreeModel</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkTreeStore.html">GtkTreeStore</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkCellRendererText.html">GtkCellRendererText</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkCellRendererToggle.html">GtkCellRendererToggle</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkTreeViewColumn.html">GtkTreeViewColumn</link>

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </section>
Packit 1470ea
</page>