Blame platform-demos/ko/combobox_multicolumn.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="combobox_multicolumn.py" xml:lang="ko">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">ComboBox(Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#menu-combo-toolbar"/>
Packit 1470ea
    <link type="next" xref="treeview_advanced_liststore.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-06-03" 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>항목 목록을 선택할 때 사용하는 위젯</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>ComboBox(두 칸 짜리)</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/combobox_multicolumn.png"/>
Packit 1470ea
  

이 ComboBox는 여러분이 바꿔서 선택한 내용을 터미널에 나타냅니다.

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
    <title>예제 결과를 만드는 코드</title>
Packit 1470ea
    from gi.repository import Gtk
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
actions = [["Select", None],
Packit 1470ea
           ["New", Gtk.STOCK_NEW],
Packit 1470ea
           ["Open", Gtk.STOCK_OPEN],
Packit 1470ea
           ["Save", Gtk.STOCK_SAVE]]
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="Welcome to GNOME", application=app)
Packit 1470ea
        self.set_default_size(200, -1)
Packit 1470ea
        self.set_border_width(10)
Packit 1470ea
Packit 1470ea
        # the data in the model, of type string on two columns
Packit 1470ea
        listmodel = Gtk.ListStore(str, str)
Packit 1470ea
        # append the data
Packit 1470ea
        for i in range(len(actions)):
Packit 1470ea
            listmodel.append(actions[i])
Packit 1470ea
Packit 1470ea
        # a combobox to see the data stored in the model
Packit 1470ea
        combobox = Gtk.ComboBox(model=listmodel)
Packit 1470ea
Packit 1470ea
        # cellrenderers to render the data
Packit 1470ea
        renderer_pixbuf = Gtk.CellRendererPixbuf()
Packit 1470ea
        renderer_text = Gtk.CellRendererText()
Packit 1470ea
Packit 1470ea
        # we pack the cell into the beginning of the combobox, allocating
Packit 1470ea
        # no more space than needed;
Packit 1470ea
        # first the image, then the text;
Packit 1470ea
        # note that it does not matter in which order they are in the model,
Packit 1470ea
        # the visualization is decided by the order of the cellrenderers
Packit 1470ea
        combobox.pack_start(renderer_pixbuf, False)
Packit 1470ea
        combobox.pack_start(renderer_text, False)
Packit 1470ea
Packit 1470ea
        # associate a property of the cellrenderer to a column in the model
Packit 1470ea
        # used by the combobox
Packit 1470ea
        combobox.add_attribute(renderer_text, "text", 0)
Packit 1470ea
        combobox.add_attribute(renderer_pixbuf, "stock_id", 1)
Packit 1470ea
Packit 1470ea
        # the first row is the active one at the beginning
Packit 1470ea
        combobox.set_active(0)
Packit 1470ea
Packit 1470ea
        # connect the signal emitted when a row is selected to the callback
Packit 1470ea
        # function
Packit 1470ea
        combobox.connect("changed", self.on_changed)
Packit 1470ea
Packit 1470ea
        # add the combobox to the window
Packit 1470ea
        self.add(combobox)
Packit 1470ea
Packit 1470ea
    def on_changed(self, combo):
Packit 1470ea
        # if the row selected is not the first one, write on the terminal
Packit 1470ea
        # the value of the first column in the model
Packit 1470ea
        if combo.get_active() != 0:
Packit 1470ea
            print("You chose " + str(actions[combo.get_active()][0]) + "\n")
Packit 1470ea
        return True
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
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="methods">
Packit 1470ea
    <title>ComboBox 위젯에 쓸만한 함수</title>
Packit 1470ea
    

ComboBox 위젯은 모델/뷰/컨트롤러 설계 방식따라 설계했습니다. 모델에는 데이터가 들어가고요, View에는 바뀜 알림을 받아서 모델의 내용을 표시합니다. 마지막으로, 컨트롤러에서는, 모델의 상태를 바꾸고 뷰가 바뀌었다는걸 알리죠. ComboBox에서 쓸만한 메서드 목록 내용은 <link xref="model-view-controller.py"/>를 확인해보시죠.

Packit 1470ea
    

45번째 줄에서, widget.connect(signal, callback function) 코드로 "changed" 시그널을 on_changed() 콜백 함수에 연결했습니다. 더 자세한 설명은 <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/GtkComboBox.html">GtkComboBox</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkListStore.html">GtkListStore</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/GtkCellRendererPixbuf.html">GtkCellRendererPixbuf</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/gtk3-Stock-Items.html">스톡 항목</link>

</item>
Packit 1470ea
      <item>

<link href="http://git.gnome.org/browse/pygobject/tree/gi/overrides/Gtk.py">pygobject - GObject 인트로스펙션 파이썬 바인딩</link>

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