修改GTKAda中按钮标签的字体大小和颜色

时间:2012-09-19 16:12:52

标签: gtk ada gnat

任何人都知道如何修改GTKAda中按钮标签的特征。

我尝试使用Pango的软件包和Style的软件包与Widget的软件包,但它们不会改变属性。

代码是这样的:

Gtk_New (Button_Select, "Select");
Modify_Font (Button_Select, From_String("Helvetica 16"));
Pack_Start (Control_Box, Button_Select, False, False, 1);

但标签“选择”的特征不会改变。

有任何想法或暗示吗?

感谢您阅读并参与我的问题。

1 个答案:

答案 0 :(得分:1)

我从PHP中的代码中找到了解决方案,我只是将命令转移到ada,但我认为留下问题并回答它是有用的。

创建按钮时,也会创建标签对象并将其添加到按钮中。可以获得标签对象--Get_Child--函数,然后将标签对象用作普通标签。

命令如下:

Set_Markup(GTk_Label(Get_Child (Button_S)), "<span weight=""bold"" color=""blue"" size=""xx-large"">It Works!!</span>");

完整的代码是下一个:

    with GLib;          use GLib;
    with Gtk.Window;    use Gtk.Window;
    with Gtk.Frame;     use Gtk.Frame;
    with Gtk.Button;    use Gtk.Button;
    with Gtk.Widget;    use Gtk.Widget;
    with Gtk.Label;     use Gtk.Label;
    with Pango.Font;    use Pango.Font;
    with Gtk.Handlers;
    with Gtk.Main;

    procedure button_label_test is
       Window          : Gtk_Window;
       Frame           : Gtk_Frame;
       Button_S        : Gtk_Button;

       package Handlers is new Gtk.Handlers.Callback (Gtk_Widget_Record);
       package Return_Handlers is
          new Gtk.Handlers.Return_Callback (Gtk_Widget_Record, Boolean);

       function Delete_Event (Widget : access Gtk_Widget_Record'Class)
          return Boolean is
       begin
          return False;
       end Delete_Event;

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

       -- This is the function to modify the characteristics of the label of the button
       procedure Clicked (Widget : access Gtk_Widget_Record'Class) is
       begin
          Set_Markup(GTk_Label(Get_Child (Button_S)), "<span weight=""bold"" color=""blue"" size=""xx-large"">It Works!!</span>");
       end Clicked;


    begin
       Gtk.Main.Init;
       Gtk.Window.Gtk_New (Window);
       Set_Default_Size (Window, 200, 200);
       Gtk.Window.Set_Title (Window, "Button Label test");
       Gtk_New (Frame);
       Gtk_New (Button_S, "Try");
       Add (Frame, Button_S);
       Add (Window, Frame);

       Return_Handlers.Connect
       (  Window,
          "delete_event",
          Return_Handlers.To_Marshaller (Delete_Event'Access)
       );
       Handlers.Connect
       (  Window,
          "destroy",
          Handlers.To_Marshaller (Destroy'Access)
       );
       Handlers.Connect
       (  Button_S,
          "clicked",
          Handlers.To_Marshaller (Clicked'Access)
       );

       Show_All (Window);
       Show (Window);

       Gtk.Main.Main;

    end button_label_test;

我认为这对某人有用。