[英]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.