<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # PyGTK 中的自定義小部件 > 原文: [http://zetcode.com/gui/pygtk/customwidget/](http://zetcode.com/gui/pygtk/customwidget/) 工具箱通常僅提供最常見的窗口小部件,例如按鈕,文本窗口小部件,滑塊等。沒有工具箱可以提供所有可能的窗口小部件。 客戶程序員必須創建更專業的小部件。 他們使用工具箱提供的繪圖工具來完成此任務。 有兩種可能:程序員可以修改或增強現有的小部件,或者可以從頭開始創建自定義小部件。 ## 刻錄小部件 這是我們從頭開始創建的小部件的示例。 可以在各種媒體刻錄應用(例如 Nero 刻錄 ROM)中找到此小部件。 `burning.py` ```py #!/usr/bin/python # -*- coding: utf-8 -*- # ZetCode PyGTK tutorial # # This example creates a burning # custom widget # # author: Jan Bodnar # website: zetcode.com # last edited: April 2011 import gtk import cairo class Burning(gtk.DrawingArea): def __init__(self, parent): self.par = parent super(Burning, self).__init__() self.num = ( "75", "150", "225", "300", "375", "450", "525", "600", "675" ) self.set_size_request(-1, 30) self.connect("expose-event", self.expose) def expose(self, widget, event): cr = widget.window.cairo_create() cr.set_line_width(0.8) cr.select_font_face("Courier", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) cr.set_font_size(11) width = self.allocation.width self.cur_width = self.par.get_cur_value() step = round(width / 10.0) till = (width / 750.0) * self.cur_width full = (width / 750.0) * 700 if (self.cur_width >= 700): cr.set_source_rgb(1.0, 1.0, 0.72) cr.rectangle(0, 0, full, 30) cr.save() cr.clip() cr.paint() cr.restore() cr.set_source_rgb(1.0, 0.68, 0.68) cr.rectangle(full, 0, till-full, 30) cr.save() cr.clip() cr.paint() cr.restore() else: cr.set_source_rgb(1.0, 1.0, 0.72) cr.rectangle(0, 0, till, 30) cr.save() cr.clip() cr.paint() cr.restore() cr.set_source_rgb(0.35, 0.31, 0.24) for i in range(1, len(self.num) + 1): cr.move_to(i*step, 0) cr.line_to(i*step, 5) cr.stroke() (x, y, width, height, dx, dy) = cr.text_extents(self.num[i-1]) cr.move_to(i*step-width/2, 15) cr.text_path(self.num[i-1]) cr.stroke() class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title("Burning") self.set_size_request(350, 200) self.set_position(gtk.WIN_POS_CENTER) self.connect("destroy", gtk.main_quit) self.cur_value = 0 vbox = gtk.VBox(False, 2) scale = gtk.HScale() scale.set_range(0, 750) scale.set_digits(0) scale.set_size_request(160, 40) scale.set_value(self.cur_value) scale.connect("value-changed", self.on_changed) fix = gtk.Fixed() fix.put(scale, 50, 50) vbox.pack_start(fix) self.burning = Burning(self) vbox.pack_start(self.burning, False, False, 0) self.add(vbox) self.show_all() def on_changed(self, widget): self.cur_value = widget.get_value() self.burning.queue_draw() def get_cur_value(self): return self.cur_value PyApp() gtk.main() ``` 我們在窗口底部放置一個`DrawingArea`并手動繪制整個窗口小部件。 所有重要的代碼都駐留在`Burning`類的`expose()`方法中。 此小部件以圖形方式顯示了介質的總容量和可供我們使用的可用空間。 該小部件由比例小部件控制。 自定義窗口小部件的最小值為 0,最大值為 750。如果值達到 700,則開始繪制紅色。 這通常表示過度燃燒。 ```py self.num = ( "75", "150", "225", "300", "375", "450", "525", "600", "675" ) ``` 這些數字顯示在刻錄小部件上。 它們顯示了介質的容量。 ```py self.cur_width = self.par.get_cur_value() ``` 這兩行從刻度小部件獲取當前數字。 我們獲得父窗口小部件,并從父窗口小部件中獲得當前值。 ```py till = (width / 750.0) * self.cur_width full = (width / 750.0) * 700 ``` 直到參數確定要繪制的總大小。 該值來自滑塊小部件。 它占整個面積的一部分。 `full`參數確定了我們開始繪制紅色的點。 ```py cr.set_source_rgb(1.0, 1.0, 0.72) cr.rectangle(0, 0, till, 30) cr.save() cr.clip() cr.paint() cr.restore() ``` 此代碼在此處繪制了一個黃色矩形,直到介質充滿為止。 ```py (x, y, width, height, dx, dy) = cr.text_extents(self.num[i-1]) cr.move_to(i*step-width/2, 15) cr.text_path(self.num[i-1]) cr.stroke() ``` 這里的代碼在刻錄小部件上繪制數字。 我們計算`TextExtents`來正確定位文本。 ```py def on_changed(self, widget): self.cur_value = widget.get_value() self.burning.queue_draw() ``` 我們從小部件中獲取值,并將其存儲在`cur_value`變量中以備后用。 我們重新繪制刻錄的小部件。 ![Burning widget](https://img.kancloud.cn/60/44/60446cb0dac131dd876db2ea997a11c1_358x228.jpg) 圖:刻錄小部件 在本章中,我們在 PyGTK 中創建了一個自定義小部件。
                  <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>

                              哎呀哎呀视频在线观看