【问题标题】:C - Gtk3- Cairo - DrawArea - Plane cartesian - How i add text?C - Gtk3- Cairo - DrawArea - 平面笛卡尔 - 我如何添加文本?
【发布时间】:2016-09-05 16:51:35
【问题描述】:

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

其他,你有关于 DrawArea- Cairo-Pango 或其他关于 gtk3 使用的 2d-3d 图形的教程指南吗?

PS:我是初学者,但是为什么人们说 gtk/c 不好呢?只是因为更复杂? 谢谢大家

【问题讨论】:

    标签: c gtk gtk3 pangocairo


    【解决方案1】:

    您可以通过切换到 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 编写的同一个程序需要两次(或更多)来编写和调试。当速度不是问题时(现代机器几乎从来不是),Python 就很棒。

    以下是一些参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多