您可以将小部件添加到单元格。正如评论中已经说明的那样,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;