C - Gtk3- Cairo - DrawArea - Plane cartesian - 如何添加文本?

C - Gtk3- Cairo - DrawArea - Plane cartesian - How i add text?

我的目的(用于个人练习)是创建一个平面笛卡尔坐标系,其中我代表一些数学函数。 为此,我需要交易坐标,如何在 DrawArea 上添加文本? 我进行了搜索,但没有找到关于使用 gtk3-C 绘制文本的任何信息(示例 ecc)。

其他,您是否有一些关于 DrawArea- Cairo-Pango 或其他关于与 gtk3 一起使用的图形 2d-3d 的教程指南?

PS:我是新手,为什么有人说gtk/c不好?只是因为更复杂? 谢谢大家

您可以通过切换到 GooCanvas 来稍微简化您的生活。可以说,使用 Cairo 和较低级别的工具更高效、更快,但可能更适合绘制小部件(按钮等)。

请注意,DrawingArea 并不是真正的绘图工具 - 它只是一个空的小部件,您可以在其中执行操作。

使用像 GooCanvas 这样的 canvas 可以减轻您的生活,通过处理重绘事件,并为您提供有用的东西,例如分层绘制、鼠标事件、打印等。

#!/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 编写的同一个程序需要两次(或更多)编写和调试。当速度不是问题时(对于现代机器几乎从来不是),Python 就很棒。

参考资料如下: