GooCanvas に切り替えることで、生活を少し簡素化できます。おそらく、Cairo および下位レベルのツールを使用する方が効率的で高速ですが、ウィジェット (ボタンなど) の描画にはより適している可能性があります。
DrawingArea は実際には描画ツールではないことに注意してください。これは、何かを実行できる空のウィジェットです。
GooCanvas のようなキャンバスを使用すると、再描画イベントを処理し、レイヤーの描画、マウス イベント、印刷などの便利な機能を提供することで、生活を楽にすることができます。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# test_goo.py
#
# Copyright 2016 John Coppens <john@jcoppens.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from gi.repository import Gtk, GooCanvas
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__()
self.connect("destroy", lambda x: Gtk.main_quit())
canvas = GooCanvas.Canvas()
root_layer = canvas.get_root_item()
# Draw a rectangle:
rect = GooCanvas.CanvasRect(
parent = root_layer,
x = 20, y = 50,
width = 60, height = 75,
line_width = 2.0,
stroke_color = "Yellow")
# Draw some text:
text1 = GooCanvas.CanvasText(
parent = root_layer,
x = 50, y = 70,
font = "Times 25",
text = "Hi there",
fill_color = "red")
self.add(canvas)
self.show_all()
def run(self):
Gtk.main()
def main(args):
mainwdw = MainWindow()
mainwdw.run()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
人は、自分がよく知らないことについてよく悪口を言います。多くの場合、私は生産性のために Python を好みます。C で記述された同じプログラムは、記述とデバッグに 2 回 (またはそれ以上) かかります。速度が問題にならない場合 (最新のマシンでは問題になることはほとんどありません)、Python は優れています。
ここにいくつかの参照があります: