如何使用 GTK 更新 gtk.Button 中的图像#

发布于 2024-09-06 16:46:32 字数 2008 浏览 7 评论 0原文

好吧,我正在开发一个简单的应用程序,用于在单声道中启动/停止和重新启动我的 LAMP(只是为了更多地了解单声道的 GUI 开发),因此为了减少按钮,我决定使用一个按钮来启动和停止服务器。在GUI设计器中我添加了一个带有图标的开始按钮,问题是更新按钮标签很容易,但是将图像更改为Stock.MediaStop就有点问题了。那么如何在点击事件的按钮中更改库存图像(它不会衡量它实际上是什么类型的事件)。这里有一些代码: 按钮的 GUI XML:

<widget class="Gtk.Button" id="button1">
       <property name="MemberName" />
       <property name="CanFocus">True</property>
       <property name="Type">TextAndIcon</property>
       <property name="Icon">stock:gtk-media-play Menu</property>
       <property name="Label" translatable="yes">Start</property>
       <property name="UseUnderline">True</property>
       <signal name="Clicked" handler="OnMysqlServerStartStop" />
</widget>

下面是 MediaDevelop 如何使用自定义文本制作 Stock 按钮:

// Container child hbox1.Gtk.Box+BoxChild
        this.hbuttonbox1 = new Gtk.HButtonBox();
        this.hbuttonbox1.Name = "hbuttonbox1";
        // Container child hbuttonbox1.Gtk.ButtonBox+ButtonBoxChild
        this.button1 = new Gtk.Button();
        this.button1.CanFocus = true;
        this.button1.Name = "button1";
        this.button1.UseUnderline = true;
        // Container child button1.Gtk.Container+ContainerChild
        Gtk.Alignment w2 = new Gtk.Alignment(0.5F, 0.5F, 0F, 0F);
        // Container child GtkAlignment.Gtk.Container+ContainerChild
        Gtk.HBox w3 = new Gtk.HBox();
        w3.Spacing = 2;
        // Container child GtkHBox.Gtk.Container+ContainerChild
        Gtk.Image w4 = new Gtk.Image();
        w4.Pixbuf = Stetic.IconLoader.LoadIcon(this, "gtk-media-play", Gtk.IconSize.Menu, 16);
        w3.Add(w4);
        // Container child GtkHBox.Gtk.Container+ContainerChild
        Gtk.Label w6 = new Gtk.Label();
        w6.LabelProp = Mono.Unix.Catalog.GetString("Start");
        w6.UseUnderline = true;
        w3.Add(w6);
        w2.Add(w3);
        this.button1.Add(w2);
        this.hbuttonbox1.Add(this.button1);

Well I was working on simple application for starting/stopping and restarting my LAMP in mono (just to get knowing the GUI development with mono bit more), so to have less buttons I decided to have one button for starting and stopping the servers. In the GUI designer I added a start button with icon, the problem is that updating the button label is easy, but changing the image to Stock.MediaStop is a bit of a problem. So How to change the stock image in a button on click event (it doesn't meter on what kind of event it is actually). Here some code:
The GUI XML for a button:

<widget class="Gtk.Button" id="button1">
       <property name="MemberName" />
       <property name="CanFocus">True</property>
       <property name="Type">TextAndIcon</property>
       <property name="Icon">stock:gtk-media-play Menu</property>
       <property name="Label" translatable="yes">Start</property>
       <property name="UseUnderline">True</property>
       <signal name="Clicked" handler="OnMysqlServerStartStop" />
</widget>

And here how MediaDevelop made the Stock button with custom text:

// Container child hbox1.Gtk.Box+BoxChild
        this.hbuttonbox1 = new Gtk.HButtonBox();
        this.hbuttonbox1.Name = "hbuttonbox1";
        // Container child hbuttonbox1.Gtk.ButtonBox+ButtonBoxChild
        this.button1 = new Gtk.Button();
        this.button1.CanFocus = true;
        this.button1.Name = "button1";
        this.button1.UseUnderline = true;
        // Container child button1.Gtk.Container+ContainerChild
        Gtk.Alignment w2 = new Gtk.Alignment(0.5F, 0.5F, 0F, 0F);
        // Container child GtkAlignment.Gtk.Container+ContainerChild
        Gtk.HBox w3 = new Gtk.HBox();
        w3.Spacing = 2;
        // Container child GtkHBox.Gtk.Container+ContainerChild
        Gtk.Image w4 = new Gtk.Image();
        w4.Pixbuf = Stetic.IconLoader.LoadIcon(this, "gtk-media-play", Gtk.IconSize.Menu, 16);
        w3.Add(w4);
        // Container child GtkHBox.Gtk.Container+ContainerChild
        Gtk.Label w6 = new Gtk.Label();
        w6.LabelProp = Mono.Unix.Catalog.GetString("Start");
        w6.UseUnderline = true;
        w3.Add(w6);
        w2.Add(w3);
        this.button1.Add(w2);
        this.hbuttonbox1.Add(this.button1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-09-13 16:46:32

我明白为什么你在这里遇到这么多麻烦了。我以为你可以只设置按钮的图像和文本属性,但似乎你可以显示标签或显示图像,但不能同时显示两者。标签会覆盖图像。我认为只有在主题设置要求时才会显示图像。这同样适用于标签。您可以将两者结合起来,仅一个标签或仅一个图像。全部由您使用的主题设置。

您走在正确的轨道上,此示例代码应该可以回答您的查询。

using System;
using Gtk;

namespace TogglePlay
{
    public class MainClass
    {
        private bool stop = false;
        private Image image;
        private Label label;

        public MainClass ()
        {
            Button button = new Button();
            VBox box = new VBox();
            image = new Image(Stock.MediaPlay, IconSize.Button);
            box.PackStart(image, true, true, 0);
            label = new Label("Start");
            box.PackStart(label, true, true, 0);
            button.Add(box);
            button.Clicked += OnButtonClicked;
            Window window = new Window("LAMP light");
            window.Add(button);
            window.DeleteEvent += DeleteWindow;
            window.ShowAll();
        }


        private void DeleteWindow(object obj, DeleteEventArgs args)
        {
            Gtk.Application.Quit();
        }

        public void OnButtonClicked(object widget, EventArgs args)
        {
            if (!stop) {
                stop = true;
                image.Stock = Stock.MediaStop;
                label.Text = "Stop";
            } else {
                stop = false;
                image.Stock = Stock.MediaPlay;
                label.Text = "Start";
            }
        }


        public static void Main(String[] args)
        {
            Gtk.Application.Init ();
            MainClass mainClass = new MainClass();
            Gtk.Application.Run ();
        }
    }
}

I can see why you are having so much trouble here. I thought you could just set the image and text properties of the button, but it seems you can have a label showing or a image showing, but not both. The label overwrites the image. I think the image will show only if the theme settings request it to. The same applies to the label. You can have a combination of the both, only a label or only a image. All set by the theme your using.

You are on the right track here, this sample code should answer your query.

using System;
using Gtk;

namespace TogglePlay
{
    public class MainClass
    {
        private bool stop = false;
        private Image image;
        private Label label;

        public MainClass ()
        {
            Button button = new Button();
            VBox box = new VBox();
            image = new Image(Stock.MediaPlay, IconSize.Button);
            box.PackStart(image, true, true, 0);
            label = new Label("Start");
            box.PackStart(label, true, true, 0);
            button.Add(box);
            button.Clicked += OnButtonClicked;
            Window window = new Window("LAMP light");
            window.Add(button);
            window.DeleteEvent += DeleteWindow;
            window.ShowAll();
        }


        private void DeleteWindow(object obj, DeleteEventArgs args)
        {
            Gtk.Application.Quit();
        }

        public void OnButtonClicked(object widget, EventArgs args)
        {
            if (!stop) {
                stop = true;
                image.Stock = Stock.MediaStop;
                label.Text = "Stop";
            } else {
                stop = false;
                image.Stock = Stock.MediaPlay;
                label.Text = "Start";
            }
        }


        public static void Main(String[] args)
        {
            Gtk.Application.Init ();
            MainClass mainClass = new MainClass();
            Gtk.Application.Run ();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文