繁体   English   中英

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

[英]How to update image in gtk.Button with GTK#

好吧,我正在开发简单的应用程序,用于启动/停止和重新启动单声道的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);

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

您在这里是正确的轨道,此示例代码应该回答您的查询。

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 ();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM