|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="widget_drawing.py" xml:lang="gl">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Widget (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#misc"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2013-02-24" status="stub"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Marta Maria Casetti</name>
|
|
Packit |
1470ea |
<email its:translate="no">mmcasetti@gmail.com</email>
|
|
Packit |
1470ea |
<years>2013</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>A widget that uses the Cairo library to draw</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Fran Dieguez</mal:name>
|
|
Packit |
1470ea |
<mal:email>frandieguez@gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012-2013.</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Widget</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/widget_drawing.png"/>
|
|
Packit |
1470ea |
Enter an angle, visualize it.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Código usado para xerar este exemplo</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
from gi.repository import cairo
|
|
Packit |
1470ea |
import sys
|
|
Packit |
1470ea |
import math
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="Choose an angle", application=app)
|
|
Packit |
1470ea |
self.set_default_size(400, 400)
|
|
Packit |
1470ea |
self.set_border_width(10)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a default angle
|
|
Packit |
1470ea |
self.angle = 360
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a spinbutton that takes the value of an angle
|
|
Packit |
1470ea |
ad = Gtk.Adjustment(360, 0, 360, 1, 0, 0)
|
|
Packit |
1470ea |
self.spin = Gtk.SpinButton(adjustment=ad, climb_rate=1, digits=0)
|
|
Packit |
1470ea |
self.spin.connect("value-changed", self.get_angle)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a drawing area for drawing whatever we want
|
|
Packit |
1470ea |
self.darea = Gtk.DrawingArea()
|
|
Packit |
1470ea |
# that we describe in the method draw(), connected to the signal "draw"
|
|
Packit |
1470ea |
self.darea.connect("draw", self.draw)
|
|
Packit |
1470ea |
# we have to request a minimum size of the drawing area, or it will
|
|
Packit |
1470ea |
# disappear
|
|
Packit |
1470ea |
self.darea.set_size_request(300, 300)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
grid.attach(self.spin, 0, 0, 1, 1)
|
|
Packit |
1470ea |
grid.attach(self.darea, 0, 1, 1, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# whenever we get a new angle in the spinbutton
|
|
Packit |
1470ea |
def get_angle(self, event):
|
|
Packit |
1470ea |
self.angle = self.spin.get_value_as_int()
|
|
Packit |
1470ea |
# redraw what is in the drawing area
|
|
Packit |
1470ea |
self.darea.queue_draw()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def draw(self, darea, cr):
|
|
Packit |
1470ea |
# a 10-pixels-wide line
|
|
Packit |
1470ea |
cr.set_line_width(10)
|
|
Packit |
1470ea |
# red
|
|
Packit |
1470ea |
cr.set_source_rgba(0.5, 0.0, 0.0, 1.0)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# get the width and height of the drawing area
|
|
Packit |
1470ea |
w = self.darea.get_allocated_width()
|
|
Packit |
1470ea |
h = self.darea.get_allocated_height()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# move to the center of the drawing area
|
|
Packit |
1470ea |
# (translate from the top left corner to w/2, h/2)
|
|
Packit |
1470ea |
cr.translate(w / 2, h / 2)
|
|
Packit |
1470ea |
# draw a line to (55, 0)
|
|
Packit |
1470ea |
cr.line_to(55, 0)
|
|
Packit |
1470ea |
# and get back to (0, 0)
|
|
Packit |
1470ea |
cr.line_to(0, 0)
|
|
Packit |
1470ea |
# draw an arc centered in the origin, 50 pixels wide, from the angle 0
|
|
Packit |
1470ea |
# (in radians) to the angle given by the spinbutton (in degrees)
|
|
Packit |
1470ea |
cr.arc(0, 0, 50, 0, self.angle * (math.pi / 180))
|
|
Packit |
1470ea |
# draw a line back to the origin
|
|
Packit |
1470ea |
cr.line_to(0, 0)
|
|
Packit |
1470ea |
# drawing the path, and keeping the path for future use
|
|
Packit |
1470ea |
cr.stroke_preserve()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# set a colour
|
|
Packit |
1470ea |
cr.set_source_rgba(0.0, 0.5, 0.5, 1.0)
|
|
Packit |
1470ea |
# and use it to fill the path (that we had kept)
|
|
Packit |
1470ea |
cr.fill()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyApplication(Gtk.Application):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self):
|
|
Packit |
1470ea |
Gtk.Application.__init__(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_activate(self):
|
|
Packit |
1470ea |
win = MyWindow(self)
|
|
Packit |
1470ea |
win.show_all()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_startup(self):
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>API References</title>
|
|
Packit |
1470ea |
Neste exemplo empregaremos o seguinte:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkWidget.html">GtkWidget</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkDrawingArea.html">GtkDrawingArea</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.tortall.net/mu/wiki/CairoTutorial">The Cairo Tutorial for Python Programmers</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|