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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Ruby GTK 中的布局管理 > 原文: [http://zetcode.com/gui/rubygtk/layoutmanagement/](http://zetcode.com/gui/rubygtk/layoutmanagement/) 在本章中,我們將展示如何在窗口或對話框上布置窗口小部件。 在設計應用的 GUI 時,我們決定使用哪些小部件以及如何在應用中組織這些小部件。 為了組織窗口小部件,我們使用稱為布局容器的專用非可見窗口小部件。 在本章中,我們提到`Gtk::Alignment`,`Gtk::Fixed`,`Gtk::VBox`和`Gtk::Grid`。 ## `Gtk::Fixed` `Gtk::Fixed`容器將子窗口小部件放置在固定位置并具有固定大小。 此容器不執行自動布局管理。 在大多數應用中,我們不使用此容器。 我們在某些特定領域使用它,例如游戲,使用圖表的應用,可以移動的可調整大小的組件(例如電子表格應用中的圖表),小型教育示例。 ```rb #!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial In this program, we lay out widgets using absolute positioning. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' class RubyApp < Gtk::Window def initialize super init_ui end def init_ui override_background_color :normal, Gdk::RGBA::new(0.2, 0.2, 0.2, 1) begin bardejov = Gdk::Pixbuf.new :file => "bardejov.jpg" rotunda = Gdk::Pixbuf.new :file => "rotunda.jpg" mincol = Gdk::Pixbuf.new :file => "mincol.jpg" rescue IOError => e puts e puts "cannot load images" exit end image1 = Gtk::Image.new :pixbuf => bardejov image2 = Gtk::Image.new :pixbuf => rotunda image3 = Gtk::Image.new :pixbuf => mincol fixed = Gtk::Fixed.new fixed.put image1, 20, 20 fixed.put image2, 40, 160 fixed.put image3, 170, 50 add fixed set_title "Fixed" signal_connect "destroy" do Gtk.main_quit end set_default_size 300, 280 window_position = :center show_all end end Gtk.init window = RubyApp.new Gtk.main ``` 在我們的示例中,我們在窗口上顯示了三個小圖像。 我們明確指定放置這些圖像的 x,y 坐標。 ```rb override_background_color :normal, Gdk::RGBA::new(0.2, 0.2, 0.2, 1) ``` 為了獲得更好的視覺體驗,我們將背景色更改為深灰色。 ```rb bardejov = Gdk::Pixbuf.new :file => "bardejov.jpg" ``` 我們將圖像從磁盤加載到`Gtk::Pixbuf`對象。 ```rb image1 = Gtk::Image.new :pixbuf => bardejov image2 = Gtk::Image.new :pixbuf => rotunda image3 = Gtk::Image.new :pixbuf => mincol ``` `Gtk::Image`是用于顯示圖像的小部件。 它在構造器中使用一個`Gdk::Pixbuf`對象。 ```rb fixed = Gtk::Fixed.new ``` 我們創建`Gtk::Fixed`容器。 ```rb fixed.put image1, 20, 20 ``` 我們將第一個圖像放置在 x = 20,y = 20 坐標處。 ```rb add fixed ``` 最后,我們將`Gtk::Fixed`容器添加到窗口中。 ![Gtk::Fixed](https://img.kancloud.cn/bf/e5/bfe5535f53fc87805ddb2314fcd83758_302x306.jpg) 圖:`Gtk::Fixed` ## 按鈕 `Gtk::Alignment`容器控制其子窗口小部件的對齊方式和大小。 ```rb #!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial In this program, we position two buttons in the bottom right corner of the window. We use horizontal and vertical boxes. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' class RubyApp < Gtk::Window def initialize super init_ui end def init_ui set_border_width 10 vbox = Gtk::Box.new :vertical, 0 hbox = Gtk::Box.new :horizontal, 5 e_space = Gtk::Alignment.new 0, 0, 0, 0 vbox.pack_start e_space, :expand => true ok_btn = Gtk::Button.new :label => "OK" ok_btn.set_size_request 70, 30 close_btn = Gtk::Button.new :label => "Close" close_btn.set_size_request 70, 30 hbox.add ok_btn hbox.add close_btn halign = Gtk::Alignment.new 1, 0, 0, 0 halign.add hbox vbox.pack_start halign, :expand => false, :fill => false, :padding => 5 add vbox set_title "Buttons" signal_connect "destroy" do Gtk.main_quit end set_default_size 260, 150 set_window_position :center show_all end end Gtk.init window = RubyApp.new Gtk.main ``` 在代碼示例中,我們在窗口的右下角放置了兩個按鈕。 為此,我們使用一個水平框,一個垂直框和兩個對齊容器。 ```rb set_border_width 10 ``` `set_border_width`在`Gtk::Window`容器窗口小部件的邊框周圍設置了一些空白。 對于我們的示例來說很重要,因為關閉按鈕不會太靠近窗口的右邊緣。 ```rb vbox = Gtk::Box.new :vertical, 0 hbox = Gtk::Box.new :horizontal, 5 ``` 將創建一個垂直和水平框。 垂直框用作我們窗口的基礎容器。 垂直框中放置有空白空間和包含兩個按鈕小部件的水平框。 ```rb e_space = Gtk::Alignment.new 0, 0, 0, 0 vbox.pack_start e_space, :expand => true ``` `Gtk::Alignment`小部件用作空白填充符。 它將按鈕推到窗口底部。 `:expand`參數將導致`Gtk::Alignment`小部件消耗分配給垂直框的所有額外空間。 ```rb hbox = Gtk::Box.new :horizontal, 5 ... ok_btn = Gtk::Button.new :label => "OK" ok_btn.set_size_request 70, 30 close_btn = Gtk::Button.new :label => "Close" close_btn.set_size_request 70, 30 hbox.add ok_btn hbox.add close_btn ``` 我們創建一個水平框,并在其中放置兩個按鈕。 `Gtk::Box`的第二個參數是子級之間的間距量。 ```rb halign = Gtk::Alignment.new 1, 0, 0, 0 halign.add hbox vbox.pack_start halign, :expand => false, :fill => false, :padding => 5 ``` 這將創建一個對齊容器,它將其子窗口小部件放在右側。 `Gtk::Alignment`容器的第一個參數是水平對齊方式。 值為 1 會將其子項(包含兩個按鈕的水平框)向右推。 對齊容器僅需要一個子窗口小部件-我們必須使用水平框。 ![Buttons](https://img.kancloud.cn/23/00/2300c8fbc23591b35b9cfbc9c597cc1d_262x176.jpg) 圖:按鈕 ## 計算器骨架 本示例借助`Gtk::Box`和`Gtk::Grid`小部件創建計算器的骨架。 ```rb #!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial In this program we create a skeleton of a calculator. We use a Gtk::Grid widget and a vertical Gtk::Box. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' class RubyApp < Gtk::Window def initialize super init_ui end def init_ui vbox = Gtk::Box.new :vertical, 2 mb = Gtk::MenuBar.new filemenu = Gtk::Menu.new file = Gtk::MenuItem.new "File" file.set_submenu filemenu mb.append file vbox.pack_start mb, :expand => false, :fill => false, :padding => 0 vbox.pack_start Gtk::Entry.new, :expand => false, :fill => false, :padding => 0 grid = Gtk::Grid.new grid.set_property "row-homogeneous", true grid.set_property "column-homogeneous", true grid.attach Gtk::Button.new(:label => "Cls"), 0, 0, 1, 1 grid.attach Gtk::Button.new(:label => "Bck"), 1, 0, 1, 1 grid.attach Gtk::Label.new, 2, 0, 1, 1 grid.attach Gtk::Button.new(:label => "Close"), 3, 0, 1, 1 grid.attach Gtk::Button.new(:label => "7"), 0, 1, 1, 1 grid.attach Gtk::Button.new(:label => "8"), 1, 1, 1, 1 grid.attach Gtk::Button.new(:label => "9"), 2, 1, 1, 1 grid.attach Gtk::Button.new(:label => "/"), 3, 1, 1, 1 grid.attach Gtk::Button.new(:label => "4"), 0, 2, 1, 1 grid.attach Gtk::Button.new(:label => "5"), 1, 2, 1, 1 grid.attach Gtk::Button.new(:label => "6"), 2, 2, 1, 1 grid.attach Gtk::Button.new(:label => "*"), 3, 2, 1, 1 grid.attach Gtk::Button.new(:label => "1"), 0, 3, 1, 1 grid.attach Gtk::Button.new(:label => "2"), 1, 3, 1, 1 grid.attach Gtk::Button.new(:label => "3"), 2, 3, 1, 1 grid.attach Gtk::Button.new(:label => "-"), 3, 3, 1, 1 grid.attach Gtk::Button.new(:label => "0"), 0, 4, 1, 1 grid.attach Gtk::Button.new(:label => "."), 1, 4, 1, 1 grid.attach Gtk::Button.new(:label => "="), 2, 4, 1, 1 grid.attach Gtk::Button.new(:label => "+"), 3, 4, 1, 1 vbox.pack_start grid, :expand => true, :fill => true, :padding => 0 add vbox set_title "Calculator" signal_connect "destroy" do Gtk.main_quit end set_default_size 300, 250 set_window_position :center show_all end end Gtk.init window = RubyApp.new Gtk.main ``` `Gtk::Grid`小部件按行和列排列小部件。 ```rb vbox = Gtk::Box.new :vertical, 2 ``` `Gtk::Box`用作我們應用的基礎容器。 框的方向是垂直的,其子框(菜單欄,條目和網格小部件)之間有 2px 的間距。 由于它是一個垂直框,因此該空間垂直放置在小部件之間。 ```rb mb = Gtk::MenuBar.new filemenu = Gtk::Menu.new file = Gtk::MenuItem.new "File" file.set_submenu filemenu mb.append file vbox.pack_start mb, :expand => false, :fill => false, :padding => 0 ``` 創建帶有一個菜單的`Gtk::MenuBar`。 它放置在垂直框內。 ```rb vbox.pack_start Gtk::Entry.new, :expand => false, :fill => false, :padding => 0 ``` `Gtk::Entry`放置在菜單欄下方。 我們將`:expand`參數設置為`false`,因為我們不想垂直擴展條目窗口小部件。 放在垂直框中的小部件從左向右拉伸。 如果要更改此設置,則需要一個附加的水平框。 ```rb grid = Gtk::Grid.new ``` `Gtk::Grid`容器已創建。 ```rb grid.set_property "row-homogeneous", true grid.set_property "column-homogeneous", true ``` 我們將行和列的均質屬性設置為`true`。 這將導致所有子項具有相同的大小。 ```rb grid.attach Gtk::Button.new(:label => "Cls"), 0, 0, 1, 1 ``` 我們在網格容器的左上角單元格上附加一個按鈕。 前兩個參數是列索引和行索引。 最后兩個參數是列跨度和行跨度。 網格內的所有小部件都占用一個單元格。 ```rb vbox.pack_start grid, :expand => true, :fill => true, :padding => 0 ``` 我們將網格小部件打包到垂直框中。 `:expand`和`:fill`選項的組合將使網格小部件及其子級占據窗口區域的大部分。 ```rb add vbox ``` 垂直框放置在`Gtk::Window`容器內。 ![Calculator skeleton](https://img.kancloud.cn/be/93/be93200d87def70f884978b9c587907b_302x276.jpg) 圖:計算機骨架 ## 窗口 在最后一個示例中,我們將使用`Gtk::Grid`容器。 此容器將其子級放置到單元格中,這些單元格由行和列的交點界定。 網格容器的`attach`方法采用五個參數。 第一個參數是附加的子窗口小部件。 接下來的兩個參數是放置子項的行和列索引。 最后兩個參數是行跨度和列跨度。 ```rb #!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial This is a more complicated layout example. We use Gtk::Alignment, Gtk::Box, and Gtk::Grid widgets. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' class RubyApp < Gtk::Window def initialize super init_ui end def init_ui set_border_width 15 grid = Gtk::Grid.new grid.set_column_spacing 5 grid.set_row_spacing 5 title = Gtk::Label.new "Windows" align1 = Gtk::Alignment.new 0, 0, 0, 0 align1.add title grid.attach align1, 0, 0, 1, 1 frame = Gtk::Frame.new frame.set_hexpand true frame.set_vexpand true grid.attach frame, 0, 1, 3, 3 vbox = Gtk::Box.new :vertical, 4 act_btn = Gtk::Button.new :label => "Activate" act_btn.set_size_request 70, 30 close_btn = Gtk::Button.new :label => "Close" close_btn.set_size_request 70, 30 vbox.add act_btn vbox.add close_btn grid.attach vbox, 3, 1, 1, 1 help_btn = Gtk::Button.new :label => "Help" help_btn.set_size_request 70, 30 align2 = Gtk::Alignment.new 0, 0, 0, 0 align2.add help_btn grid.attach align2, 0, 4, 1, 1 ok_btn = Gtk::Button.new :label => "OK" ok_btn.set_size_request 70, 30 grid.attach ok_btn, 3, 4, 1, 1 add grid set_title "Windows" signal_connect "destroy" do Gtk.main_quit end set_default_size 350, 300 set_window_position :center show_all end end Gtk.init window = RubyApp.new Gtk.main ``` 該代碼在 Ruby GTK 中創建了一個真實世界的窗口。 ```rb grid = Gtk::Grid.new grid.set_column_spacing 5 grid.set_row_spacing 5 ``` 創建`Gtk::Grid`容器的實例。 該容器將小部件打包為行和列。 我們在行和列之間設置一些空間。 ```rb title = Gtk::Label.new "Windows" align1 = Gtk::Alignment.new 0, 0, 0, 0 align1.add title grid.attach align1, 0, 0, 1, 1 ``` 我們創建一個標簽小部件。 將此小部件放置在`Gtk::Alignment`小部件中,以便使其與標簽上的空格左側對齊。 網格容器的`attach`方法將標簽放入其左上角的單元格中。 標簽將占據一個單元格。 ```rb frame = Gtk::Frame.new frame.set_hexpand true frame.set_vexpand true grid.attach frame, 0, 1, 3, 3 ``` 框架小部件位于`column = 0`和`row = 1`處。 它跨越三行和樹列。 `set_hexpand`和`set_vexpand`方法將窗口小部件設置為占用任何可用的額外水平和垂直空間。 當窗口增長時,框架小部件也增長; 其他小部件保留其大小。 ```rb vbox = Gtk::Box.new :vertical, 4 act_btn = Gtk::Button.new :label => "Activate" act_btn.set_size_request 70, 30 close_btn = Gtk::Button.new :label => "Close" close_btn.set_size_request 70, 30 vbox.add act_btn vbox.add close_btn grid.attach vbox, 3, 1, 1, 1 ``` 創建兩個按鈕并將其放置在垂直框中。 “到”框位于框架小部件旁邊。 ```rb help_btn = Gtk::Button.new :label => "Help" help_btn.set_size_request 70, 30 align2 = Gtk::Alignment.new 0, 0, 0, 0 align2.add help_btn grid.attach align2, 0, 4, 1, 1 ``` 幫助按鈕位于對齊容器內部,該對齊按鈕將其對齊到網格容器旁邊放置的單元格的左側。 較早的`set_hexpand`方法調用使框架窗口小部件可擴展; 它還會影響框架窗口小部件占用的列中的窗口小部件。 因此,我們需要使用`Gtk::Alignment`小部件來保持按鈕的大小不變,并將其向左對齊。 ![Windows](https://img.kancloud.cn/b9/1b/b91b0a9260e178e3eb5929ca6a82553f_352x326.jpg) 圖:窗口 在 Ruby GTK 教程的這一部分中,我們提到了小部件的布局管理。
                  <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>

                              哎呀哎呀视频在线观看