精灵中vala lambdas的替代品
精灵中缺少lambdas会给工作流带来一些问题。有一个我无法回避的特殊情况 我在这个特殊练习中的目标是创建一个笔记本,其中有一个按钮,单击该按钮将使用户转到同一笔记本的下一页。共有四个页面,最后一个页面上的按钮会将用户带回第一个页面(与第一个页面类似) 在瓦拉,这似乎可以很容易地用兰博达斯做到。我尝试了使用类内共享变量的建议方法,但问题是,尽管我通过button.click.connect访问了函数中的变量(callback?仍然不能完全确定特定的术语),但它仍然不能被识别为笔记本 我的做法如下:精灵中vala lambdas的替代品,lambda,gtk,genie,Lambda,Gtk,Genie,精灵中缺少lambdas会给工作流带来一些问题。有一个我无法回避的特殊情况 我在这个特殊练习中的目标是创建一个笔记本,其中有一个按钮,单击该按钮将使用户转到同一笔记本的下一页。共有四个页面,最后一个页面上的按钮会将用户带回第一个页面(与第一个页面类似) 在瓦拉,这似乎可以很容易地用兰博达斯做到。我尝试了使用类内共享变量的建议方法,但问题是,尽管我通过button.click.connect访问了函数中的变量(callback?仍然不能完全确定特定的术语),但它仍然不能被识别为笔记本 我的做法如下
[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\u current\u页面(2)
notebook没有继承笔记本的属性
我希望你能给我一些关于如何避免这个问题的建议,因为我已经没有想法了。我曾尝试创建函数来替换不推荐使用的语法+=def()
,但也遇到了类似的问题
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 ()
我认为唯一的选择就是这样。不受支持。您最好只使用更受支持的Vala。谢谢TingPing。但我们想学的是精灵而不是瓦拉。在许多答案中,人们都对精灵提出建议。请不要管我们的选择。另一方面,您的错误是因为您使用了“var”实例笔记本,这使得它只能在Testwindow过程中使用。在我的示例中,您可以看到我的实例笔记本没有“var”,对于finish,笔记本页面从0开始。非常好!现在,我应该什么时候避免
var
?当使用“var”进行避免时,您可能只会在相同的def过程或init块中使用此变量。但如果你要在其他“def”中使用。对不起,我的英语是。var
声明变量而不必指定确切的类型,但是您已经使用notebook:Gtk.notebook
声明了变量。您还希望notebook
在类的整个范围内可用,但在init
函数中再次声明它意味着它仅在该函数的范围内可用,而不是整个类。顺便说一下,您应该使用construct()
而不是init
init
在第一次调用类时只调用一次,因此如果您有多个窗口,那么事情就会变得混乱。