0

我有这个代码

主文件

With Gtk.Main;         Use Gtk.Main;
With Gtk.Window;       Use Gtk.Window;
With Gtk.Button;       Use Gtk.Button;
With Gtk.Widget;       Use Gtk.Widget;
With Gtk.Grid;         Use Gtk.Grid;

Procedure Main is

   Win : Gtk_Window;
   Button : Gtk_Button;
   Button2 : Gtk_Button;
   Button3 : Gtk_Button;
   Grid : Gtk_Grid;

begin
   Init;

   Gtk_New (Win);
   Win.Set_Default_Size (Width  => 380,
                         Height => 502);

   Gtk_New (Button,"Button");

   Gtk_New (Button2,"Button2");
   
   Gtk_New (Button3,"Button3");

   Gtk_New (Grid);

   Grid.Attach (Button,0,0);

   Grid.Attach (Button2,0,100);
   
   Grid.Attach (Button3,75,20);
   
   Win.Add (Grid);

   Win.Show_All;
   Gtk.Main.Main;
end Main;

在这里,我希望我的第一个按钮在最左边的顶部,我的第三个按钮在右边但在底部,我的第二个按钮必须在底部。我尝试了几乎所有的方法,但仍然徒劳无功我无法正确对齐我的小部件,所以有人知道如何将我的所有小部件与 Gtk_Grid 对齐。

4

1 回答 1

0

您可以将小部件添加到单元格。正如评论中已经说明的那样,Top和的Left参数Attach表示单元格索引(行/列),而不是像素。请参阅下面的注释示例。

如果要使用坐标定位小部件,则可以使用 GTK Fixed 容器。但是请注意,这个容器几乎从来都不是定位小部件的好解决方案;原则上,小部件应始终使用布局容器 HBox、VBox、Table、Grid 和 Layout 来定位。这是为了确保无论用户的屏幕分辨率和屏幕大小如何,都可以正确组合小部件。在 Fixed 小部件的描述中强调了这一点:

对于大多数应用程序,您不应该使用此容器!它使您不必了解其他 GTK+ 容器,但会导致应用程序损坏。

我添加了一个示例,显示了容器小部件 Grid 和 Fixed 的用法。在这些示例中,我将 GTK 代码移动到一个单独的包中,以便添加一个Destroy_Event_Callback依次调用Gtk.Main.Main_Quit以停止 GTK 事件循环并在您关闭窗口时正确退出程序的程序。

app.adb(使用 GTK 固定,参见手册GtkAda 源码

with Gtk.Main;   use Gtk.Main;
with Gtk.Window; use Gtk.Window;
with Gtk.Button; use Gtk.Button;
with Gtk.Widget; use Gtk.Widget;
with Gtk.Fixed;  use Gtk.Fixed;

package body App is

   procedure Destroy_Event_Callback
     (Widget : access Gtk.Widget.Gtk_Widget_Record'Class);

   ---------
   -- Run --
   ---------

   procedure Run is

      Win      : Gtk_Window;
      Button_1 : Gtk_Button;
      Button_2 : Gtk_Button;
      Button_3 : Gtk_Button;
      Fixed    : Gtk_Fixed;

   begin
      Gtk.Main.Init;

      Gtk_New (Win);
      Win.Set_Default_Size (Width => 380, Height => 502);
      Win.On_Destroy (Call => Destroy_Event_Callback'Access);

      Gtk_New (Button_1, "Button 1");
      Gtk_New (Button_2, "Button 2");
      Gtk_New (Button_3, "Button 3");

      Gtk_New (Fixed);

      --  Add the buttons to the container with their
      --  top-left corner at the specified coordinate.
      Fixed.Put (Button_1, X =>   0, Y =>   0);
      Fixed.Put (Button_2, X =>  50, Y => 100);   
      Fixed.Put (Button_3, X => 200, Y => 100);      

      Win.Add (Fixed);
      Win.Show_All;

      Gtk.Main.Main;

   end Run;

   ----------------------------
   -- Destroy_Event_Callback --
   ----------------------------

   procedure Destroy_Event_Callback
     (Widget : access Gtk.Widget.Gtk_Widget_Record'Class)
   is
   begin
      Gtk.Main.Main_Quit;
   end Destroy_Event_Callback;

end App;

app.adb(使用 GTK Grid,参见手册GtkAda 源代码

with Gtk.Main;   use Gtk.Main;
with Gtk.Window; use Gtk.Window;
with Gtk.Button; use Gtk.Button;
with Gtk.Widget; use Gtk.Widget;
with Gtk.Grid;   use Gtk.Grid;

package body App is

   procedure Destroy_Event_Callback
     (Widget : access Gtk.Widget.Gtk_Widget_Record'Class);
   
   ---------
   -- Run --
   ---------

   procedure Run is
   
      Win      : Gtk_Window;
      Button_1 : Gtk_Button;
      Button_2 : Gtk_Button;
      Button_3 : Gtk_Button;
      Grid     : Gtk_Grid;

   begin
      Gtk.Main.Init;

      Gtk_New (Win);
      Win.Set_Default_Size (Width => 380, Height => 502);
      Win.On_Destroy (Call => Destroy_Event_Callback'Access);
   
      Gtk_New (Button_1, "Button 1");
      Gtk_New (Button_2, "Button 2");
      Gtk_New (Button_3, "Button 3");

      Gtk_New (Grid);
   
      --  Stretch the grid to the size of the window.
      Grid.Set_Hexpand (True);      
      Grid.Set_Vexpand (True);
   
      --  Make all cells have the same width/height.
      Grid.Set_Column_Homogeneous (True);
      Grid.Set_Row_Homogeneous (True);

      --  Insert the buttons into the grid.
      --    
      --      Left:    0      1      2
      --   Top:    +------+------+------+
      --        0  | Btn1 |      |      |
      --           +------+------+------+
      --        1  |      | Btn3 | Btn2 |
      --           +------+------+------+
      --
      Grid.Attach (Button_1, Left => 0, Top => 0);
      Grid.Attach (Button_2, Left => 2, Top => 1);
      Grid.Attach (Button_3, Left => 1, Top => 1);   
   
      Win.Add (Grid);
      Win.Show_All;
   
      Gtk.Main.Main;
   
   end Run;   
   
   ----------------------------
   -- Destroy_Event_Callback --
   ----------------------------

   procedure Destroy_Event_Callback
     (Widget : access Gtk.Widget.Gtk_Widget_Record'Class)
   is
   begin
      Gtk.Main.Main_Quit;
   end Destroy_Event_Callback;

end App;

应用程序广告

package App is

   procedure Run;

end App;

主文件

with App;

procedure Main is
begin
   App.Run;
end Main;
于 2022-03-04T17:47:36.697 回答