Create TOC

2009년 5월 12일

Python/PyGTK

PyGTK 예제 소스를 순수 API를 호출하는 방법과 GtkBuilder를 사용한 방법으로 각각 구현해본다.

Hello, World

아래와 같은 모습을 가지도록 프로그램을 수정한다.

API 호출

코드

hello.py


#!/usr/bin/python
# -*- coding: utf-8 -*-
import gtk

class HelloWorld :
    def hello(self, widget, data=None) :
        print 'Hello, World'

    def delete_event(self, widget, event, data=None):
        print 'delete event occurred'
        return False

    def destroy(self, widget, data=None) :
        gtk.main_quit()

    def __init__(self) :
        self._win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self._win.connect('delete_event', self.delete_event)
        self._win.connect('destroy', self.destroy)
        self._win.set_border_width(10)
        self._btnhello = gtk.Button('Hello World')
        self._btnhello.connect('clicked', self.hello, None)
        self._btnhello.connect_object('clicked', gtk.Widget.destroy, self._win)
        self._win.add(self._btnhello)

    def main(self) :
        self._win.show_all()
        gtk.main()

def main() :
    w = HelloWorld()
    w.main()

if __name__ == '__main__' :
    main()

GtkBuilder

GtkBuilder를 사용하면 ui 구성 코드를 xml로 분리할 수 있다.

코드

hello.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<interface>
  <object class="GtkWindow" id="HelloWindow">
    <property name="border_width">10</property>
    <signal name="delete_event" handler="delete_event" />
    <signal name="destroy" handler="destroy" />
    <child>
      <object class="GtkButton" id="BtnHello">
        <property name="label">Hello, world</property>
        <signal name="clicked" handler="hello" />
      </object>
    </child>
  </object>
</interface>

hello2.py

>#!/usr/bin/python # -*- coding: utf-8 -*- import gtk class HelloWorld : def hello(self, widget, data=None) : print 'Hello, World' def delete_event(self, widget, event, data=None): print 'delete event occurred' return False def destroy(self, widget, data=None) : gtk.main_quit() def __init__(self) : builder = gtk.Builder() builder.add_from_file('hello2.xml') builder.connect_signals(self) self._win = builder.get_object('HelloWindow') self._btnhello = builder.get_object('BtnHello') self._btnhello.connect_object('clicked', gtk.Widget.destroy, self._win) def main(self) : self._win.show_all() gtk.main() def main() : w = HelloWorld() w.main() if __name__ == '__main__' : main()

Hello, World 업그레이드

아래와 같은 모습을 가지도록 프로그램을 수정한다.

API 호출

코드

hello_upgrade.py

>#!/usr/bin/python # -*- coding: utf-8 -*- import gtk class HelloWorld2 : def callback(self, widget, data) : print 'Hello again - %s was pressed ' % data def delete_event(self, widget, event, data=None): gtk.main_quit() return False def __init__(self) : self._win = gtk.Window(gtk.WINDOW_TOPLEVEL) self._win.set_title('Hello Buttons!') self._win.connect('delete_event', self.delete_event) self._win.set_border_width(10) self._box1 = gtk.HBox(False, 0) self._win.add(self._box1) self._button1 = gtk.Button('Button 1') self._button1.connect('clicked', self.callback, 'button 1') self._box1.pack_start(self._button1, True, True, 0) self._button2 = gtk.Button('Button 2') self._button2.connect('clicked', self.callback, 'button 2') self._box1.pack_start(self._button2, True, True, 0) def main(self) : self._win.show_all() gtk.main() def main() : w = HelloWorld2() w.main() if __name__ == '__main__' : main()

GtkBuilder

코드

hello2_upgrade.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<interface>
  <object class="GtkWindow" id="HelloWindow">
    <property name="title">Hello Buttons!</property>
    <property name="border_width">10</property>
    <signal name="delete_event" handler="delete_event" />
    <child>
      <object class="GtkHBox" id="box1">
        <child>
          <object class="GtkButton" id="button1">
            <property name="label">Button 1</property>
            <signal name="clicked" handler="callback" />
          </object>
        </child>
        <child>
          <object class="GtkButton" id="button2">
            <property name="label">Button 2</property>
            <signal name="clicked" handler="callback" />
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

hello2_upgrade.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import gtk

class HelloWorld2 :
    def callback(self, widget, data = None) :
        print 'Hello again - %s was pressed ' % widget.get_name()

    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def __init__(self) :
        builder = gtk.Builder()
        builder.add_from_file('hello2_upgrade.xml')
        builder.connect_signals(self)
        self._win = builder.get_object('HelloWindow')

    def main(self) :
        self._win.show_all()
        gtk.main()

def main() :
    w = HelloWorld2()
    w.main()

if __name__ == '__main__' :
    main()

Reference