morris555's diary

高校生のブログです。

python de Gtk+3 #4

次はボックス!!

#!/use/bin/env python
# -*- coding:utf-8 -*-

from gi.repository import Gtk


class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title=u'Box')

        self.box = Gtk.Box(spacing=5)
        self.add(self.box)

        self.button1 = Gtk.Button(label=u'Hello')
        self.button1.connect('clicked', self.on_button1_clicked)
        self.box.pack_start(self.button1, True, True, 0)

        self.button2 = Gtk.Button(label=u'World')
        self.button2.connect('clicked', self.on_button2_clicked)
        self.box.pack_start(self.button2, True, True, 0)

        self.button3 = Gtk.Button(label=u'QUIT')
        self.button3.connect('clicked', self.on_button3_clicked)
        self.box.pack_start(self.button3, True, True, 0)

        self.connect('delete-event', Gtk.main_quit)
        self.show_all()

    def on_button1_clicked(self, widget):
        print u'Hello'

    def on_button2_clicked(self, widget):
        print u'World'

    def on_button3_clicked(self, widget):
        Gtk.main_quit()

if __name__ == '__main__':
    MainWindow()
    Gtk.main()

簡単でしたww
Boxの引数は「spacing=*」で間隔、「Gtk.Orientation.VERTICAL」で、縦に配置できます(ちなみにdefaultでは、Gtk.Orientation.HORIZONTAL, spacing=0)になっています。