2016-01-01 2 views
2

Отсутствие lambdas в Genie создает некоторые проблемы для рабочего процесса. Есть особое обстоятельство, что я не могу обойти.Альтернативы vala lambdas в Genie

Моя цель в этом конкретном упражнении - создать ноутбук, в котором есть кнопка, которая после щелчка приведет пользователя к следующей странице того же ноутбука. Есть четыре страницы, а кнопка на последнем возвращает пользователя к первому (аналогично here).

Кажется, что в вала это легко можно сделать с лямбдами. Я попробовал предложенный подход 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(), и я наткнулся на подобные проблемы.

+0

Возможно, вам лучше использовать только Валу, которая больше поддерживается. – TingPing

+0

Спасибо TingPing. Но мы пытаемся узнать Гени, а не Валу. Во многих ответах люди советуют снова Genie. Пожалуйста, дайте нам один с нашим выбором. – txasatonga

ответ

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

Я думаю, что единственной альтернативой является это. Не поддерживается.

+1

С другой стороны, ваша ошибка была вызвана тем, что вы записали экземпляр ноутбука с использованием «var», и это делает возможным использование только в процедуре Testwindow. В моем примере вы можете увидеть я экземпляр ноутбука без «var» – txasatonga

+1

И для завершения страницы ноутбуков начинаются с 0. – txasatonga

+0

Отлично! Теперь, когда мне следует избегать '' 'var'''? –

 Смежные вопросы

  • Нет связанных вопросов^_^