3

Genie 中缺少 lambda 给工作流程带来了一些问题。有一种特殊情况我无法规避。

我在这个特定练习中的目标是创建一个笔记本,其中有一个按钮,单击该按钮会将用户带到同一笔记本的下一页。有四个页面,最后一个页面上的按钮将用户带回第一个页面(类似于此处的那个)。

似乎在 vala 中,这可以用 lambdas 轻松完成。我尝试了这里建议的使用类内共享变量的方法,但问题是,尽管我设法通过 button.click.connect 访问调用(回调?仍然不完全确定特定术语)的函数中的变量,它仍然不能被识别为笔记本电脑。

这是我的方法:

[indent=4]
uses Gtk

class TestWindow : Window
    notebook:Gtk.Notebook

    init
        // General characteristics of the window
        title = "Gtk Containers"
        default_height = 250
        default_width = 250
        window_position = WindowPosition.CENTER
        destroy.connect(Gtk.main_quit)

        // Now building the notebook
        var notebook = new Gtk.Notebook()
        var label1 = new Gtk.Label("Page one")
        var label2 = new Gtk.Label("Page two")
        var label3 = new Gtk.Label("Page three")
        var label4 = new Gtk.Label("Page four")

        var child1 = new Button.with_label ("Go to next page")
        child1.clicked += def ()
            notebook.set_current_page(2)
        var child2 = new Button.with_label ("Go to next page")
        child2.clicked += def ()
            notebook.set_current_page(3)
        var child3 = new Button.with_label ("Go to next page")
        child3.clicked += def ()
            notebook.set_current_page(4)
        var child4 = new Button.with_label ("Go to first page")
        child4.clicked += def ()
            notebook.set_current_page(1)

        notebook.append_page(child1, label1)
        notebook.append_page(child2, label2)
        notebook.append_page(child3, label3)
        notebook.append_page(child4, label4)

        // Now building the grid
        var grid = new Grid()
        var button1 = new Gtk.Button.with_mnemonic("Button_1")
        var button2 = new Button.with_mnemonic("Button 2")

        // Attaching all elements into the grid
        grid.attach(notebook, 0,0,2,1)
        grid.attach(button1, 0,1,1,1)
        grid.attach(button2, 1,1,1,1)
        add(grid)


init
    Gtk.init (ref args)
    var test = new TestWindow ()
    test.show_all ()
    Gtk.main ()

我在运行时得到的错误:

(gtkcontainerswithgrid:23039): Gtk-CRITICAL **: gtk_notebook_append_page: assertion 'GTK_IS_NOTEBOOK (notebook)' failed

所以我想在notebook.set_current_page(2)笔记本中没有继承笔记本的属性。

我会很感激一些关于如何规避这个问题的指示,因为我已经没有想法了。我尝试创建函数来替换不推荐使用的语法+= def(),并且偶然发现了类似的问题。

4

1 回答 1

3
uses Gtk
class TestWindow : Window
    notebook:Gtk.Notebook
    init
        // General characteristics of the window
        title = "Gtk Containers"
        default_height = 250
        default_width = 250
        window_position = WindowPosition.CENTER
        destroy.connect(Gtk.main_quit)

        // Now building the notebook
        notebook = new Gtk.Notebook()
        var label1 = new Gtk.Label("Page one")
        var label2 = new Gtk.Label("Page two")
        var label3 = new Gtk.Label("Page three")
        var label4 = new Gtk.Label("Page four")

        var child1 = new Button.with_label ("Go to next page")
        child1.clicked.connect (childclicked1)
        var child2 = new Button.with_label ("Go to next page")
        child2.clicked.connect (childclicked2)
        var child3 = new Button.with_label ("Go to next page")
        child3.clicked.connect (childclicked3)
        var child4 = new Button.with_label ("Go to first page")
        child4.clicked.connect (childclicked4)


        notebook.append_page(child1, label1)
        notebook.append_page(child2, label2)
        notebook.append_page(child3, label3)
        notebook.append_page(child4, label4)

        // Now building the grid
        var grid = new Grid()
        var button1 = new Gtk.Button.with_mnemonic("Button_1")
        var button2 = new Button.with_mnemonic("Button 2")

        // Attaching all elements into the grid
        grid.attach(notebook, 0,0,2,1)
        grid.attach(button1, 0,1,1,1)
        grid.attach(button2, 1,1,1,1)
        add(grid)

    def childclicked1()
        notebook.set_current_page(1)

    def childclicked2()
        notebook.set_current_page(2)

    def childclicked3()
        notebook.set_current_page(3)

    def childclicked4()
        notebook.set_current_page(0)

init
    Gtk.init (ref args)
    var test = new TestWindow ()
    test.show_all ()
    Gtk.main ()

我认为唯一的选择是这个。不支持。

于 2016-01-02T08:36:43.590 回答