如何将回调函数连接到其信号
我创建了两个显示 Gtk_Radio_Button 的按钮和一个显示 GtK_label 的按钮,但是当我单击按钮时没有任何反应。我不明白为什么它不起作用,但代码编译并运行时没有发现任何错误。
main_Program.adb
With Gtk.Window; Use Gtk.Window;
With Gtk.Button; Use Gtk.Button;
With Gtk.Grid; Use Gtk.Grid;
With file; Use file;
Procedure main_program is
Bouton : Gtk_Button;
Bouton2 : Gtk_Button;
Win : Gtk_Window;
Test : Test_Record;
begin
Gtk.Main.Init;
Init_Grid (Container => Test);
Gtk_New (Win);
Win.Add (Test.Grid);
Win.Set_Default_Size (Width => 600,
Height => 400);
Gtk_New (Bouton,Label => "Bouton");
Test.Grid.Attach (Bouton,0,0);
P.Connect (Widget => Bouton,
Name => Signal_Clicked,
Marsh => Test2.P.To_Marshaller (Init_Button'Access),
After => False);
Gtk_New (Bouton2,Label => "Bouton2");
Test.Grid.Attach (Bouton2,50,4);
P.Connect (Widget => Bouton2,
Name => Signal_Clicked,
Marsh => Test2.P.To_Marshaller (Init_Text'Access),
After => False);
Win.Show_All;
Main;
end Main_Program;
file.ads
With Gtk.Label; Use Gtk.Label;
With Gtk.Radio_Button; Use Gtk.Radio_Button;
With Gtk.Grid; Use Gtk.Grid;
With Gtk.Handlers;
Package file is
type Test_Record is record
Text : Gtk_Label;
Grid : Gtk_Grid;
Button_Radio : Gtk_Radio_Button;
end record;
Procedure Init_Text ( Self : access Gtk_Widget_Record'Class );
-- Callback for create the text
Procedure Init_Button (Self : access Gtk_Widget_Record'Class );
-- Callback for Initialize the Radio Button;
Procedure Init_Grid (Container : out Test_Record);
-- Initialize the Gtk.Grid.Gtk_Grid
Package P is new Gtk.Handlers.Callback (Gtk_Widget_Record);
Use P;
end file;
file.adb
With file; Use file;
Package body file is
Procedure Init_Grid ( Container : out Test_Record ) is
begin
Gtk_New (Container.Grid);
end Init_Grid;
Procedure Init_Button ( Self : access Gtk_Widget_Record'Class ) is
V : Test_Record;
begin
Init_Grid (Container => V);
Gtk_New (V.Button_Radio,
Group => null,
Label => "Button Radio");
V.Grid.Attach (V.Button_Radio,0,50);
V.Grid.Show;
end Init_Button;
Procedure Init_Text (Self : access Gtk_Widget_Record'Class) is
V : Test_Record;
begin
Init_Grid (Container => V);
Gtk_New (V.Text);
V.Text.Set_Label ("Hello,World");
V.Grid.Attach (V.Text,150,0);
V.Grid.Show;
end Init_Text;
end file;
我希望当我单击第一个按钮时,它会显示一个单选按钮,当我单击第二个按钮时,它会显示一个标签。
i created two buttons a that display a Gtk_Radio_Button and a button that display a GtK_label but when i click on buttons nothing happens. I don't understand why it doesn't work yet the code compiles and runs without finding any error.
main_Program.adb
With Gtk.Window; Use Gtk.Window;
With Gtk.Button; Use Gtk.Button;
With Gtk.Grid; Use Gtk.Grid;
With file; Use file;
Procedure main_program is
Bouton : Gtk_Button;
Bouton2 : Gtk_Button;
Win : Gtk_Window;
Test : Test_Record;
begin
Gtk.Main.Init;
Init_Grid (Container => Test);
Gtk_New (Win);
Win.Add (Test.Grid);
Win.Set_Default_Size (Width => 600,
Height => 400);
Gtk_New (Bouton,Label => "Bouton");
Test.Grid.Attach (Bouton,0,0);
P.Connect (Widget => Bouton,
Name => Signal_Clicked,
Marsh => Test2.P.To_Marshaller (Init_Button'Access),
After => False);
Gtk_New (Bouton2,Label => "Bouton2");
Test.Grid.Attach (Bouton2,50,4);
P.Connect (Widget => Bouton2,
Name => Signal_Clicked,
Marsh => Test2.P.To_Marshaller (Init_Text'Access),
After => False);
Win.Show_All;
Main;
end Main_Program;
file.ads
With Gtk.Label; Use Gtk.Label;
With Gtk.Radio_Button; Use Gtk.Radio_Button;
With Gtk.Grid; Use Gtk.Grid;
With Gtk.Handlers;
Package file is
type Test_Record is record
Text : Gtk_Label;
Grid : Gtk_Grid;
Button_Radio : Gtk_Radio_Button;
end record;
Procedure Init_Text ( Self : access Gtk_Widget_Record'Class );
-- Callback for create the text
Procedure Init_Button (Self : access Gtk_Widget_Record'Class );
-- Callback for Initialize the Radio Button;
Procedure Init_Grid (Container : out Test_Record);
-- Initialize the Gtk.Grid.Gtk_Grid
Package P is new Gtk.Handlers.Callback (Gtk_Widget_Record);
Use P;
end file;
file.adb
With file; Use file;
Package body file is
Procedure Init_Grid ( Container : out Test_Record ) is
begin
Gtk_New (Container.Grid);
end Init_Grid;
Procedure Init_Button ( Self : access Gtk_Widget_Record'Class ) is
V : Test_Record;
begin
Init_Grid (Container => V);
Gtk_New (V.Button_Radio,
Group => null,
Label => "Button Radio");
V.Grid.Attach (V.Button_Radio,0,50);
V.Grid.Show;
end Init_Button;
Procedure Init_Text (Self : access Gtk_Widget_Record'Class) is
V : Test_Record;
begin
Init_Grid (Container => V);
Gtk_New (V.Text);
V.Text.Set_Label ("Hello,World");
V.Grid.Attach (V.Text,150,0);
V.Grid.Show;
end Init_Text;
end file;
I want that when I click on the first button it shows me a radio button and when I click on the second button it shows me a label.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会考虑不创建/销毁标签和单选按钮,而只是切换它们的可见性。此外,我还建议从 GTK 窗口类型派生主窗口,并始终在请求时正确终止应用程序(请参阅下面示例中的
Destroy_Event_Callback
)。app.ads
app-main_window.ads
app-main_window.adb
main.adb
I would consider to not create/destroy the label and radio button, but just toggle their visibility instead. Furthermore, I would also recommend to derive the main window from the GTK window type and always properly terminate the application when requested (see
Destroy_Event_Callback
in the example below).app.ads
app-main_window.ads
app-main_window.adb
main.adb