我的目的(作为个人练习)是创建一个平面笛卡尔,我在其中表示一些数学函数。因此,我需要交易坐标,我如何在DrawArea上添加文本?我搜索了,但我没有找到关于用gtk3-C绘制文本的任何东西(示例ecc)。
其他,你有一些教程指南绘制区域-开罗- pango或其他关于图形2d-3d使用与gtk3?
PS:我是初学者,但是为什么人们说gtk/c不好?只是因为它更复杂吗?由于人你可以通过切换到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就是很棒。
以下是一些参考:
- Gnome-developer GooCanvas参考 一个更完整的GooCanvas示例,C
- Lazka的Python优秀参考
- 特别是GooCanvas2参考
- 如何在DrawingArea (in C)中绘制文本