使用 GtkAda 单击按钮后如何更改标签的文本

How do I change the text of a label after clicking a button using GtkAda

我有这个代码,它有 1 个标签和 3 个按钮。如果我按下某个按钮,我想要的是更改标签的文本。如果我单击第一个按钮,标签文本将变为“Hello”,如果我单击第二个按钮,标签文本将变为“World”,最后如果我单击第三个按钮,标签文本应变为“Hello World”。

with Gdk.Event;       use Gdk.Event;

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

procedure Main is

   Win   : Gtk_Window;
   Label : Gtk_Label;
   Box   : Gtk_Vbox;
   Button: Gtk_Button;
   Button2: Gtk_Button;
   Button3: Gtk_Button;
   Grid  : Gtk_Grid;

   function Delete_Event_Cb
     (Self  : access Gtk_Widget_Record'Class;
      Event : Gdk.Event.Gdk_Event)
      return Boolean;

   ---------------------
   -- Delete_Event_Cb --
   ---------------------

   function Delete_Event_Cb
     (Self  : access Gtk_Widget_Record'Class;
      Event : Gdk.Event.Gdk_Event)
      return Boolean
   is
      pragma Unreferenced (Self, Event);
   begin
      Gtk.Main.Main_Quit;
      return True;
   end Delete_Event_Cb;

begin
   --  Initialize GtkAda.
   Gtk.Main.Init;

   --  Create a window with a size of 100x120
   Gtk_New (Win);
   Win.Set_Default_Size (100, 120);

   --  Create a box to organize vertically the contents of the window
   Gtk_New_Vbox (Box);
   Win.Add (Box);


   --  Add a label
   Gtk_New (Label, "Hello world.");
   Box.Add (Label);
 
   -- Add a Grid
   Gtk_New (Grid);
   Box.Add (Grid);
    
   -- Add the first button to the grid 
   Gtk_New (Button, "Hello");
   Grid.Attach (Button, 0, 0, 1, 1);

   -- Add Second button to the grid
   Gtk_New (Button2, "World");
   Grid.Attach (Button2, 1, 0, 1, 1);

   -- Add the third button to the grid
   Gtk_New (Button3, "Hello World!");
   Grid.Attach (Button3, 0, 1, 2, 1);

   -- Stop the Gtk process when closing the window
   Win.On_Delete_Event (Delete_Event_Cb'Unrestricted_Access);

   --  Show the window and present it
   Win.Show_All;
   Win.Present;

   --  Start the Gtk+ main loop
   Gtk.Main.Main;
end Main;

我应该添加什么才能做到这一点?

您必须向按钮添加回调以修改标签,其方式与在 GTK 中所做的非常相似。我有一段时间没有使用 GTKAda,但它应该可以工作:

with Gdk.Event;       use Gdk.Event;

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

procedure Main is

   Win   : Gtk_Window;
   Label : Gtk_Label;
   Box   : Gtk_Vbox;
   Button: Gtk_Button;
   Button2: Gtk_Button;
   Button3: Gtk_Button;
   Grid  : Gtk_Grid;

   function Delete_Event_Cb
     (Self  : access Gtk_Widget_Record'Class;
      Event : Gdk.Event.Gdk_Event)
      return Boolean;

   ---------------------
   -- Delete_Event_Cb --
   ---------------------

   function Delete_Event_Cb
     (Self  : access Gtk_Widget_Record'Class;
      Event : Gdk.Event.Gdk_Event)
      return Boolean
   is
      pragma Unreferenced (Self, Event);
   begin
      Gtk.Main.Main_Quit;
      return True;
   end Delete_Event_Cb;

   -- Callback to update label when buttons were clicked
   procedure Update_Label(Self : access Gtk_Button_Record'Class) is
   begin
      if Self = Button then
         Set_Text(Label, "Hello");
      elsif Self = Button2 then
         Set_Text(Label, "World");
      else
         Set_Text(Label, "Hello World");
      end if;
   end Update_Label;

begin
   --  Initialize GtkAda.
   Gtk.Main.Init;

   --  Create a window with a size of 100x120
   Gtk_New (Win);
   Win.Set_Default_Size (100, 120);

   --  Create a box to organize vertically the contents of the window
   Gtk_New_Vbox (Box);
   Win.Add (Box);


   --  Add a label
   Gtk_New (Label, "Hello world.");
   Box.Add (Label);
 
   -- Add a Grid
   Gtk_New (Grid);
   Box.Add (Grid);
    
   -- Add the first button to the grid 
   Gtk_New (Button, "Hello");
   -- Attach the callback to the button's event On_Click
   On_Clicked(Button, Update_Label'Access);
   Grid.Attach (Button, 0, 0, 1, 1);

   -- Add Second button to the grid
   Gtk_New (Button2, "World");
   -- Attach the callback to the second button's event On_Click
   On_Clicked(Button2, Update_Label'Access);
   Grid.Attach (Button2, 1, 0, 1, 1);

   -- Add the third button to the grid
   Gtk_New (Button3, "Hello World!");
   -- Attach the callback to the third button's event On_Click
   On_Clicked(Button3, Update_Label'Access);
   Grid.Attach (Button3, 0, 1, 2, 1);

   -- Stop the Gtk process when closing the window
   Win.On_Delete_Event (Delete_Event_Cb'Unrestricted_Access);

   --  Show the window and present it
   Win.Show_All;
   Win.Present;

   --  Start the Gtk+ main loop
   Gtk.Main.Main;
end Main;

@thindil 给了我一个关于如何使用 set_text 过程更改标签文本的想法,但是每当我 运行 他的代码时,这条消息总是显示:“子程序不能比访问更深输入。

我没有获取嵌套过程,而是创建了一个新包。这是更新后的代码:

main.adb

with Gdk.Event;       use Gdk.Event;

with Gtk.Box;         use Gtk.Box;
with Gtk.Label;       use Gtk.Label;
with Gtk.Widget;      use Gtk.Widget;
with Gtk.Main;
with Gtk.Window;      use Gtk.Window;
with Gtk.Button;      use Gtk.Button;


with button_click;    use button_click;
procedure Main is

   Win   : Gtk_Window;
   Box   : Gtk_Vbox;
   Box2  : Gtk_Vbox;
   Box3  : Gtk_Hbox;
   Box4  : Gtk_Hbox;
   Box5  : Gtk_Hbox;
   

   function Delete_Event_Cb
     (Self  : access Gtk_Widget_Record'Class;
      Event : Gdk.Event.Gdk_Event)
      return Boolean;

   ---------------------
   -- Delete_Event_Cb --
   ---------------------

   function Delete_Event_Cb
     (Self  : access Gtk_Widget_Record'Class;
      Event : Gdk.Event.Gdk_Event)
      return Boolean
   is
      pragma Unreferenced (Self, Event);
   begin
      Gtk.Main.Main_Quit;
      return True;
   end Delete_Event_Cb;
   
   

begin
   --  Initialize GtkAda.
   Gtk.Main.Init;

   --  Create a window with a size of 400x400
   Gtk_New (Win);
   Win.Set_Default_Size (400, 400);

   --  Create a box to organize vertically the contents of the window
   Gtk_New_Vbox (Box);
   Win.Add (Box);

   --  Add a label inside Vbox Box
   Gtk_New (button_click.Label, "Try Pressing the buttons :)");
   Box.Add (button_click.Label);

   -- Adding Vbox Box2 inside Box
   Gtk_New_Vbox (Box2);
   Box.Add (Box2);

   -- Adding Hbox Box3 inside Box2
   Gtk_New_Hbox (Box3);
   Box2.Add (Box3);

   -- Adding Hbox Box4 inside Vbox Box3
   Gtk_New_Hbox (Box4);
   Box3.Add (Box4);

   -- Adding Hbox Box5 inside Vbox Box3
   Gtk_New_Hbox (Box5);
   Box3.Add (Box5);

   -- Placing Button inside Hbox Box3
   Gtk_New (button_click.Button, "Hello");
   Box4.Add (button_click.Button);
   On_Clicked(button_click.Button, button_clicked'Access);

   -- Placing Button2 inside Hbox Box4
   Gtk_New (button_click.Button2, "World");
   Box5.Add (button_click.Button2);
   On_Clicked(button_click.Button2, button_clicked'Access);
   
   -- Placing Button3 inside Vbox Box2
   Gtk_New (button_click.Button3, "Hello World");
   Box2.Add (button_click.Button3);
   On_Clicked(button_click.Button3, button_clicked'Access);


   -- Stop the Gtk process when closing the window
   Win.On_Delete_Event (Delete_Event_Cb'Unrestricted_Access);

   --  Show the window and present it
   Win.Show_All;
   Win.Present;

   --  Start the Gtk+ main loop
   Gtk.Main.Main;
end Main;

button_click.ads

with Gtk.Button;  use Gtk.Button;
with Gtk.Label;       use Gtk.Label;

package button_click is
   
   Button: Gtk_Button;
   Button2: Gtk_Button;
   Button3: Gtk_Button;
   Label : Gtk_Label;
   procedure button_clicked (Self : access Gtk_Button_Record'Class);
end button_click;

button_click.adb

with Gtk.Button;  use Gtk.Button;
with Gtk.Label;       use Gtk.Label;

package body button_click is
  
   procedure button_clicked (Self : access Gtk_Button_Record'Class) is
   begin
      if Self = Button then
         Set_Text(Label, "Hello");
      elsif Self = Button2 then
         Set_Text(Label, "World");
      else
         Set_Text(Label, "Hello World");
      end if;
   end button_clicked;

end button_click;