Paepoi » PyGObject Tips » Gtk(PyGObject) Tips | オーナメント
Gtk(PyGObject) Tips | オーナメント
# 最終更新日 2019.08.18
2019 年現在の仕様に追記と書き換え。
GtkFrame は見ての通りのコンテナ、ダイアログに少し色気を付けたい場合とかに。
ただ GNOME 標準アプリでコレを使っているものは現在一つもない。
GtkFrame のサブクラスですが用途がまるで違うめずらしい存在。
そのためか devhelp ではコンテナに分類されている。
GNOME でも GtkBox, GtkPopoverMenu 等で実は大活躍している目立たない功労者。
2019 年現在の仕様に追記と書き換え。
GtkFrame
オーナメントとは、いわゆる装飾GtkFrame は見ての通りのコンテナ、ダイアログに少し色気を付けたい場合とかに。
ただ GNOME 標準アプリでコレを使っているものは現在一つもない。
#!/usr/bin/env python3
import sys, gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Win(Gtk.ApplicationWindow):
'''
ぶっちゃけ一昔前の古い装飾って感じ
'''
def __init__(self, app):
Gtk.ApplicationWindow.__init__(self, application=app, title='Py', resizable=False)
# RadioButtons
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
root = None
for s in ('HONDA PCX', 'YAMAHA TRICITY', 'SUZUKI BURGMAN'):
radio = Gtk.RadioButton(label=s, group=root)
if not root:
root = radio
vbox.pack_start(radio, False, False, 0)
# GtkFrame
frame = Gtk.Frame(label='スクーター', margin=10)
frame.add(vbox)
self.add(frame)
self.show_all()
class App(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_startup(self):
Gtk.Application.do_startup(self)
Win(self)
def do_activate(self):
self.props.active_window.present()
app = App()
app.run(sys.argv)
GtkAspectFrame
親ウインドウをリサイズしても指定されたアスペクト比を保持するコンテナ。GtkFrame のサブクラスですが用途がまるで違うめずらしい存在。
そのためか devhelp ではコンテナに分類されている。
#!/usr/bin/env python3
import sys, gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Win(Gtk.ApplicationWindow):
'''
16:9 のバッテンを保持しつづけるウインドウ
label プロパティを指定しなければ単なる四角になる
'''
def __init__(self, app):
Gtk.ApplicationWindow.__init__(self, application=app, title='Py')
# GtkDrawingArea
da = Gtk.DrawingArea()
da.connect('draw', self.on_draw)
# GtkAspectFrame
frame = Gtk.AspectFrame(
xalign=0.5, # 中心に
yalign=0.5, # 中心に
ratio=16.0/9.0, # アスペクト比
obey_child=False # False にしないと 1:1 を強制
)
frame.add(da)
self.add(frame)
self.resize(300, 150)
self.show_all()
def on_draw(self, widget, cr):
width = widget.get_allocated_width()
height = widget.get_allocated_height()
# cairo_t を引き伸ばす
cr.scale(width, height)
# バッテンを書く
cr.set_source_rgb(0, 0, 0)
cr.move_to(0, 0)
cr.line_to(1, 1)
cr.move_to(1, 0)
cr.line_to(0, 1)
cr.set_line_width(0.2)
cr.stroke()
class App(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_startup(self):
Gtk.Application.do_startup(self)
Win(self)
def do_activate(self):
self.props.active_window.present()
app = App()
app.run(sys.argv)
GtkSeparator
単なるセパレータ。GNOME でも GtkBox, GtkPopoverMenu 等で実は大活躍している目立たない功労者。
#!/usr/bin/env python3
import sys, gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Win(Gtk.ApplicationWindow):
'''
ちょっと見え辛いよねコレ
'''
def __init__(self, app):
Gtk.ApplicationWindow.__init__(self, application=app, title='Py')
# GtkSeparator
separator = Gtk.Separator(orientation=Gtk.Orientation.VERTICAL)
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
hbox.pack_start(Gtk.Label(label='ココに→'), False, False, 0)
hbox.pack_start(separator, False, False, 0)
hbox.pack_start(Gtk.Label(label='←セパレータがある'), False, False, 0)
self.add(hbox)
self.show_all()
class App(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_startup(self):
Gtk.Application.do_startup(self)
Win(self)
def do_activate(self):
self.props.active_window.present()
app = App()
app.run(sys.argv)
Copyright(C) sasakima-nao All rights reserved 2002 --- 2025.