Sie können Ihr Leben ein wenig vereinfachen, indem Sie zu GooCanvas wechseln. Die Verwendung von Cairo und Tools auf niedrigerer Ebene ist effizienter und schneller, aber möglicherweise besser zum Zeichnen von Widgets (Schaltflächen und dergleichen) geeignet.
Beachten Sie, dass DrawingArea ist nicht wirklich ein Zeichenwerkzeug -. Es ist nur ein leeres Widget, wo Sie tun können, Dinge in
eine Leinwand wie GooCanvas kann Ihr Leben erleichtert, indem die Nachzug-Veranstaltungen kümmern, und geben Ihnen nützliche Dinge wie in Schichten, Mausereignisse, Druck Detailzeichnung usw.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# test_goo.py
#
# Copyright 2016 John Coppens <[email protected]>
#
# 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))
Die Leute sagen oft schlechte Dinge über Dinge, die sie wissen nicht wirklich. Häufig bevorzuge ich Python wegen der Produktivität - das gleiche Programm, das in C geschrieben wird, braucht zweimal (oder mehr) zum Schreiben und Debuggen. Wenn Geschwindigkeit kein Problem ist (mit modernen Maschinen ist es fast nie), ist Python einfach großartig.
Hier sind einige Referenzen: