Gtk.Entry无法通过其他方法设置set_text

时间:2018-11-26 21:21:17

标签: gtk3 vala

vala和Gtk的新手。 我无法调用Gtk.Entry的set_text方法来设置另一个方法的文本。这是我尝试的示例代码。我可以在Activate()方法中使用set_text,但不能使用tryThis()方法。

using Gtk;
public class MyApplication : Gtk.Application {

public MyApplication () {
    Object(application_id: "testing.my.application",
    flags: ApplicationFlags.FLAGS_NONE);
}

protected override void activate () {

    Gtk.ApplicationWindow window = new Gtk.ApplicationWindow (this);
    window.set_default_size (800, 600);
    window.window_position = WindowPosition.CENTER;
    window.set_border_width(10);

    Gtk.HeaderBar headerbar = new Gtk.HeaderBar();
    headerbar.show_close_button = true;
    headerbar.title = "Window";
    window.set_titlebar(headerbar);

    //Entry is initialized here
    Gtk.Entry entry = new Gtk.Entry();
    entry.set_text ("Before button click");

    //Button is initialized and connect to method
    Gtk.Button but = new Gtk.Button.with_label("Click me");
    but.clicked.connect(tryThis);

    Gtk.Box vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
    vbox.pack_start(entry, false, false, 10);
    vbox.pack_start(but, false, false, 20);

    window.add(vbox);
    window.show_all ();
}

private void tryThis() {
    Gtk.Entry entry = new Gtk.Entry();
    //This is not working!!
    entry.set_text ("After button click");
    message("%s -", "I am here");
}

public static int main (string[] args) {
    MyApplication app = new MyApplication ();
    return app.run (args);
}

}

1 个答案:

答案 0 :(得分:3)

问题是范围问题。因此,library(data.table) nm1 <- unique(sub(".*_", "", names(layout_1)[-1])) melt(setDT(layout_1), measure = patterns("test_1", "test_2"), value.name = c('test_1_result', 'test_2_result'), variable.name = 'time')[, time := nm1[time]][] 在该方法而不是整个类的范围内创建一个activateentry创建tryThis的新实例,并将其分配给该方法范围内的变量Gtk.Entry,而不是整个类。

此示例可解决您的问题,但不是最佳解决方案,如示例后所讨论:

entry

您应该注意:

  • using Gtk; public class MyApplication : Gtk.Application { Gtk.Entry entry; public MyApplication () { Object(application_id: "testing.my.application", flags: ApplicationFlags.FLAGS_NONE); } protected override void activate () { Gtk.ApplicationWindow window = new Gtk.ApplicationWindow (this); window.set_default_size (800, 600); window.window_position = WindowPosition.CENTER; window.set_border_width(10); Gtk.HeaderBar headerbar = new Gtk.HeaderBar(); headerbar.show_close_button = true; headerbar.title = "Window"; window.set_titlebar(headerbar); //Entry is initialized here entry = new Gtk.Entry(); entry.set_text ("Before button click"); //Button is initialized and connect to method Gtk.Button but = new Gtk.Button.with_label("Click me"); but.clicked.connect(tryThis); Gtk.Box vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); vbox.pack_start(entry, false, false, 10); vbox.pack_start(but, false, false, 20); window.add(vbox); window.show_all (); } private void tryThis() { entry.set_text ("After button click"); message("%s -", "I am here"); } public static int main (string[] args) { MyApplication app = new MyApplication (); return app.run (args); } } 在类定义的开始处被entry引入整个类的范围
  • Gtk.Entry entry;已从entry = new Gtk.Entry ();中删除,因为在调用tryThis时实例化了该条目

这可行,但是从长远来看,最好将窗口与应用程序分开。因此,例如,使用activate实例化一个新的activate。 Vala还包括用于Gtk模板的代码生成例程。这样,您可以使用XML或GUI工具Glade定义窗口及其子窗口小部件,然后将Vala代码附加到Vala属性MainApplicationWindow[GtkTemplate][GtkChild]