【问题标题】:How to pack a button in a HeaderBar using Genie?如何使用 Genie 在 HeaderBar 中打包按钮?
【发布时间】:2016-03-28 08:36:17
【问题描述】:

背景

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

代码

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(见下文)时,出现错误:

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 时会出现类似的错误。

问题

  1. 我认为 pack_start 应该与 HeaderBars 一起使用是错误的吗?

第二个小问题是关于 Stock 图标的使用。由于某种原因,我无法访问 Stock.Open。​​

最后,关于 HeaderBar 是否还有其他信息来源? Valadoc 在这个主题中很少见(没有示例或模板)。

【问题讨论】:

    标签: gtk3 vala genie


    【解决方案1】:

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

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

    正确的代码是:

    headerbar.pack_start (open_button)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多