如何使用Genie在HeaderBar中打包按钮? [英] How to pack a button in a HeaderBar using Genie?

查看:93
本文介绍了如何使用Genie在HeaderBar中打包按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是改进一个小文本编辑器作为练习.添加HeaderBar后,它运行良好,但是我找不到在其中打包按钮的方法.

My aim is to improve a little text editor as an exercise. It is running fine after the HeaderBar was added, however I can't find a way to pack buttons in it.

uses
    Granite.Widgets
    Gtk

init
    Gtk.init (ref args)

    var app = new Application ()
    app.show_all ()
    Gtk.main ()

// This class holds all the elements from the GUI
class Application : Gtk.Window

    _view:Gtk.TextView

    construct ()

        // Prepare Gtk.Window:
        this.window_position = Gtk.WindowPosition.CENTER
        this.destroy.connect (Gtk.main_quit)
        this.set_default_size (400, 400)


        // Headerbar definition
        headerbar:Gtk.HeaderBar = new Gtk.HeaderBar()
        headerbar.show_close_button = true
        headerbar.set_title("My text editor")

        // Headerbar buttons
        var open_button = new ToolButton.from_stock(Stock.Open)

        // Add everything to the toolbar
        open_button.pack_start ()
        show_all ()
        this.set_titlebar(headerbar)

        // Box:
        box:Gtk.Box = new Gtk.Box (Gtk.Orientation.VERTICAL, 1)
        this.add (box)

        // A ScrolledWindow:
        scrolled:Gtk.ScrolledWindow = new Gtk.ScrolledWindow (null, null)
        box.pack_start (scrolled, true, true, 0)

        // The TextView:
        _view = new Gtk.TextView ()
        _view.set_wrap_mode (Gtk.WrapMode.WORD)
        _view.buffer.text = "Lorem Ipsum"
        scrolled.add (_view)

        // A Button:
        button:Gtk.Button = new Gtk.Button.with_label ("Print content to
                                                        stdout")
        box.pack_start (button, false, true, 0)
        button.clicked.connect (clicked)

    // This is a simple stub function to take care of the click
    def clicked ()
        stdout.puts (_view.buffer.text)
        stdout.putc ('\n')

错误

使用pack_start(见下文)时,出现错误:

Error

When pack_start (see below) is used, I get the error:

text_editor-exercise_7_1.gs:136.3-136.39: error: Access to instance member `Gtk.HeaderBar.pack_start' denied

当我使用HeaderBar.pack_start或button_name.pack_start时,也会发生类似的错误.

Similar errors occur when I use HeaderBar.pack_start or button_name.pack_start.

  1. 我是否错误地认为应该将Pack_start与HeaderBars一起使用?

第二个较小的问题是有关股票"图标的使用.由于某些原因,我无法访问Stock.Open.​​

A second smaller problem is regarding the use of Stock icons. For some reason I can't access Stock.Open.

最后,HeaderBar上还有其他信息来源吗?瓦拉多克(Valadoc)在这个主题上是稀疏的(没有示例或模板).

Finally, is there any other source of information on HeaderBar? Valadoc is sparse in this subject (there are no examples or templates).

推荐答案

您正尝试在按钮而不是标题栏上调用pack_start:

You are trying to call pack_start on the button, not on the headerbar:

// Add everything to the toolbar
open_button.pack_start ()

正确的代码是:

headerbar.pack_start (open_button)

这篇关于如何使用Genie在HeaderBar中打包按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆