<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # PyGTK 中的 Cario 繪圖 > 原文: [http://zetcode.com/gui/pygtk/drawing/](http://zetcode.com/gui/pygtk/drawing/) 在 PyGTK 編程教程的這一部分中,我們將使用 Cairo 庫進行一些繪制。 Cairo 是用于創建 2D 矢量圖形的庫。 我們可以使用它來繪制自己的小部件,圖表以及各種效果或動畫。 ## 簡單繪圖 描邊操作繪制形狀的輪廓,填充操作填充形狀的內部。 接下來,我們將演示這兩個操作。 `simpledrawing.py` ```py #!/usr/bin/python # ZetCode PyGTK tutorial # # This code example draws a circle # using the cairo library # # author: jan bodnar # website: zetcode.com # last edited: February 2009 import gtk import math class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title("Simple drawing") self.resize(230, 150) self.set_position(gtk.WIN_POS_CENTER) self.connect("destroy", gtk.main_quit) darea = gtk.DrawingArea() darea.connect("expose-event", self.expose) self.add(darea) self.show_all() def expose(self, widget, event): cr = widget.window.cairo_create() cr.set_line_width(9) cr.set_source_rgb(0.7, 0.2, 0.0) w = self.allocation.width h = self.allocation.height cr.translate(w/2, h/2) cr.arc(0, 0, 50, 0, 2*math.pi) cr.stroke_preserve() cr.set_source_rgb(0.3, 0.4, 0.6) cr.fill() PyApp() gtk.main() ``` 在我們的示例中,我們將繪制一個圓并將其用純色繪制。 ```py darea = gtk.DrawingArea() ``` 我們將在`DrawingArea`小部件上進行繪制操作。 ```py darea.connect("expose-event", self.expose) ``` 我們都使用作為`expose-event`信號處理器的方法進行繪制。 ```py cr = widget.window.cairo_create() ``` 我們從繪圖區域的`gdk.Window`創建 cairo 上下文對象。 上下文是用于在所有`Drawable`對象上繪制的對象。 ```py cr.set_line_width(9) ``` 我們將線條的寬度設置為 9 像素。 ```py cr.set_source_rgb(0.7, 0.2, 0.0) ``` 我們將顏色設置為深紅色。 ```py w = self.allocation.width h = self.allocation.height cr.translate(w/2, h/2) ``` 我們得到繪圖區域的寬度和高度。 我們將原點移動到窗口的中間。 ```py cr.arc(0, 0, 50, 0, 2*math.pi) cr.stroke_preserve() ``` 我們繪制一個圓形的外部形狀。 紅色。 `stroke_preserve()`根據當前的線寬,線連接,線帽和筆劃線設置描邊當前路徑。 與`stroke()`不同,它在 cairo 上下文中保留路徑。 ```py cr.set_source_rgb(0.3, 0.4, 0.6) cr.fill() ``` 這會用一些藍色填充圓圈的內部。 ![Simple drawing](https://img.kancloud.cn/fd/97/fd972be02d38aa6eecaec0b853a4ee46_238x178.jpg) 圖:簡單 drawing ## 基本形狀 下一個示例將一些基本形狀繪制到窗口上。 `basicshapes.py` ```py #!/usr/bin/python # ZetCode PyGTK tutorial # # This code example draws basic shapes # with the cairo library # # author: jan bodnar # website: zetcode.com # last edited: February 2009 import gtk import math class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title("Basic shapes") self.set_size_request(390, 240) self.set_position(gtk.WIN_POS_CENTER) self.connect("destroy", gtk.main_quit) darea = gtk.DrawingArea() darea.connect("expose-event", self.expose) self.add(darea) self.show_all() def expose(self, widget, event): cr = widget.window.cairo_create() cr.set_source_rgb(0.6, 0.6, 0.6) cr.rectangle(20, 20, 120, 80) cr.rectangle(180, 20, 80, 80) cr.fill() cr.arc(330, 60, 40, 0, 2*math.pi) cr.fill() cr.arc(90, 160, 40, math.pi/4, math.pi) cr.fill() cr.translate(220, 180) cr.scale(1, 0.7) cr.arc(0, 0, 50, 0, 2*math.pi) cr.fill() PyApp() gtk.main() ``` 在此示例中,我們將創建一個矩形,一個正方形,一個圓形,一個弧形和一個橢圓形。 ```py cr.rectangle(20, 20, 120, 80) cr.rectangle(180, 20, 80, 80) cr.fill() ``` 這些線繪制一個矩形和一個正方形。 ```py cr.arc(330, 60, 40, 0, 2*math.pi) cr.fill() ``` 此處`arc()`方法繪制一個完整的圓。 ```py cr.scale(1, 0.7) cr.arc(0, 0, 50, 0, 2*math.pi) cr.fill() ``` 如果要繪制橢圓形,請先進行一些縮放。 在這里`scale()`方法縮小 y 軸。 ![Basic shapes](https://img.kancloud.cn/05/75/05757bb2044e3e4bd7f719ae5a16c73c_398x268.jpg) 圖:基本形狀 ## 色彩 顏色是代表紅色,綠色和藍色(RGB)強度值的組合的對象。 Cario 有效 RGB 值在 0 到 1 的范圍內。 `colors.py` ```py #!/usr/bin/python # ZetCode PyGTK tutorial # # This program shows how to work # with colors in cairo # # author: jan bodnar # website: zetcode.com # last edited: February 2009 import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title("Colors") self.resize(360, 100) self.set_position(gtk.WIN_POS_CENTER) self.connect("destroy", gtk.main_quit) darea = gtk.DrawingArea() darea.connect("expose-event", self.expose) self.add(darea) self.show_all() def expose(self, widget, event): cr = widget.window.cairo_create() cr.set_source_rgb(0.2, 0.23, 0.9) cr.rectangle(10, 15, 90, 60) cr.fill() cr.set_source_rgb(0.9, 0.1, 0.1) cr.rectangle(130, 15, 90, 60) cr.fill() cr.set_source_rgb(0.4, 0.9, 0.4) cr.rectangle(250, 15, 90, 60) cr.fill() PyApp() gtk.main() ``` 我們用三種不同的顏色繪制三個矩形。 ```py cr.set_source_rgb(0.2, 0.23, 0.9) ``` `set_source_rgb()`方法為 Cario 上下文設置顏色。 該方法的三個參數是顏色強度值。 ```py cr.rectangle(10, 15, 90, 60) cr.fill() ``` 我們創建一個矩形形狀,并用先前指定的顏色填充它。 ![Colors](https://img.kancloud.cn/0d/9d/0d9d4d946d8488415c2cabc1813881ec_368x128.jpg) 圖:顏色 ## 透明矩形 透明性是指能夠透視材料的質量。 了解透明度的最簡單方法是想象一塊玻璃或水。 從技術上講,光線可以穿過玻璃,這樣我們就可以看到玻璃后面的物體。 在計算機圖形學中,我們可以使用 alpha 合成來實現透明效果。 Alpha 合成是將圖像與背景組合以創建部分透明外觀的過程。 合成過程使用 Alpha 通道。 (wikipedia.org,answers.com) `transparentrectangles.py` ```py #!/usr/bin/python # ZetCode PyGTK tutorial # # This program shows transparent # rectangles using cairo # # author: jan bodnar # website: zetcode.com # last edited: February 2009 import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title("Transparent rectangles") self.resize(590, 90) self.set_position(gtk.WIN_POS_CENTER) self.connect("destroy", gtk.main_quit) darea = gtk.DrawingArea() darea.connect("expose-event", self.expose) self.add(darea) self.show_all() def expose(self, widget, event): cr = widget.window.cairo_create() for i in range(1, 11): cr.set_source_rgba(0, 0, 1, i*0.1) cr.rectangle(50*i, 20, 40, 40) cr.fill() PyApp() gtk.main() ``` 在示例中,我們將繪制十個具有不同透明度級別的矩形。 ```py cr.set_source_rgba(0, 0, 1, i*0.1) ``` `set_source_rgba()`方法的最后一個參數是 alpha 透明度。 ![Transparent rectangles](https://img.kancloud.cn/bc/20/bc205ca9edb22541d97c754edbaf8df9_598x118.jpg) 圖:透明矩形 ## 靈魂伴侶 在下一個示例中,我們在窗口上繪制一些文本。 `soulmate.py` ```py #!/usr/bin/python # ZetCode PyGTK tutorial # # This program draws text # using cairo # # author: jan bodnar # website: zetcode.com # last edited: February 2009 import gtk import cairo class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title("Soulmate") self.set_size_request(370, 240) self.set_position(gtk.WIN_POS_CENTER) self.connect("destroy", gtk.main_quit) darea = gtk.DrawingArea() darea.connect("expose-event", self.expose) self.add(darea) self.show_all() def expose(self, widget, event): cr = widget.window.cairo_create() cr.set_source_rgb(0.1, 0.1, 0.1) cr.select_font_face("Purisa", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) cr.set_font_size(13) cr.move_to(20, 30) cr.show_text("Most relationships seem so transitory") cr.move_to(20, 60) cr.show_text("They're all good but not the permanent one") cr.move_to(20, 120) cr.show_text("Who doesn't long for someone to hold") cr.move_to(20, 150) cr.show_text("Who knows how to love without being told") cr.move_to(20, 180) cr.show_text("Somebody tell me why I'm on my own") cr.move_to(20, 210) cr.show_text("If there's a soulmate for everyone") PyApp() gtk.main() ``` 我們顯示 Natasha Bedingfields Soulmate 歌曲的部分歌詞。 ```py cr.select_font_face("Purisa", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) ``` 在這里,我們指定使用的字體。 ```py cr.set_font_size(13) ``` 我們指定字體的大小。 ```py cr.move_to(20, 30) ``` 我們移動到要繪制文本的位置。 ```py cr.show_text("Most relationships seem so transitory") ``` `show_text()`方法將文本繪制到窗口上。 ![Soulmate](https://img.kancloud.cn/8b/2f/8b2fd13da57f4658f0ad226807c1053d_378x268.jpg) 圖:靈魂伴侶 在 PyGTK 編程庫的這一章中,我們使用 Cairo 圖形庫進行繪制。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看