PyGobject(九十六)Cairo系列——画给定角度的弧

例子

使用一个Gtk.SpinButton,输入0到360度之间的一个度数,然后在DrawingArea中画出相应的弧度
这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/6
# section 146
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib
import sys

TITLE = "DrawArc"
DESCRIPTION = """
Enter an angle, visualize it.
"""


class MyWindow(Gtk.ApplicationWindow):
    def __init__(self, app):
        Gtk.Window.__init__(self, title="Choose an angle", application=app)
        self.set_default_size(400, 400)
        self.set_border_width(10)

        # a default angle
        self.angle = 360

        grid = Gtk.Grid()

        # a spinbutton that takes the value of an angle
        ad = Gtk.Adjustment(360, 0, 360, 1, 0, 0)
        self.spin = Gtk.SpinButton(adjustment=ad, climb_rate=1, digits=0)
        self.spin.connect("value-changed", self.get_angle)

        # a drawing area for drawing whatever we want
        self.darea = Gtk.DrawingArea()
        # that we describe in the method draw(), connected to the signal "draw"
        self.darea.connect("draw", self.draw)
        # we have to request a minimum size of the drawing area, or it will
        # disappear
        self.darea.set_size_request(300, 300)

        grid.attach(self.spin, 0, 0, 1, 1)
        grid.attach(self.darea, 0, 1, 1, 1)

        self.add(grid)

    # whenever we get a new angle in the spinbutton
    def get_angle(self, event):
        self.angle = self.spin.get_value_as_int()
        # redraw what is in the drawing area
        self.darea.queue_draw()

    def draw(self, darea, cr):
        # a 10-pixels-wide line
        cr.set_line_width(10)
        # red
        cr.set_source_rgba(0.5, 0.0, 0.0, 1.0)

        # get the width and height of the drawing area
        w = self.darea.get_allocated_width()
        h = self.darea.get_allocated_height()

        # move to the center of the drawing area
        # (translate from the top left corner to w/2, h/2)
        cr.translate(w / 2, h / 2)
        # draw a line to (55, 0)
        cr.line_to(55, 0)
        # and get back to (0, 0)
        cr.line_to(0, 0)
        # draw an arc centered in the origin, 50 pixels wide, from the angle 0
        # (in radians) to the angle given by the spinbutton (in degrees)
        cr.arc(0, 0, 50, 0, self.angle * (GLib.PI / 180))
        # draw a line back to the origin
        cr.line_to(0, 0)
        # drawing the path, and keeping the path for future use
        cr.stroke_preserve()

        # set a colour
        cr.set_source_rgba(0.0, 0.5, 0.5, 1.0)
        # and use it to fill the path (that we had kept)
        cr.fill()


class MyApplication(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self)

    def do_activate(self):
        win = MyWindow(self)
        win.show_all()

    def do_startup(self):
        Gtk.Application.do_startup(self)


def main():
    app = MyApplication()
    exit_status = app.run(sys.argv)
    sys.exit(exit_status)


if __name__ == '__main__':
    main()





代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值