button - 使用 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;
我应该添加什么来实现这一点?
解决方案
@thindil 给了我一个关于如何使用该set_text
过程更改标签文本的想法,但是每当我运行他的代码时,此消息总是显示:“子程序不能比访问类型更深”。
我没有获得嵌套过程,而是创建了一个新包。这是更新的代码:
主文件
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;
推荐阅读
- google-bigquery - 在 bigquery 中滚动在前几天也很活跃的活跃用户
- python - 如何检查xpath中的路径是否存在?
- r - 将两个子模式上的字符串拆分为 data.frame
- python - 如何使我的代码的最终结果成为加密文本?
- php - echo 和 php 不起作用,我该如何解决?
- applescript - 在 AppleScript 中重复提取两个字符串之间的文本
- charts - 图表中的 Configmap 复制精确结构
- reactjs - 使用变量类型反应 Typescript 组件(来自道具)
- html - 使用 flexbox 使动态对象居中
- html - 使用 position: relative 将文本和图像定位为水平居中