gpt4 book ai didi

lambda - Genie 中 vala lambda 的替代品

转载 作者:行者123 更新时间:2023-12-02 09:21:16 24 4
gpt4 key购买 nike

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

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

似乎在 vala 中,可以使用 lambda 轻松完成此操作。我尝试了建议的方法 here使用类中共享的变量,但问题是,虽然我设法通过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(),但我遇到了类似的问题。

最佳答案

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

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

关于lambda - Genie 中 vala lambda 的替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34555485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com