Gtk 如何使用工具按钮点击信号(在标题栏中)?
创建简单的ToolButton时,可以使用中的clicked.connect界面。将按钮添加到的界面类似于该示例中所示的界面。然而,处理点击连接的方式似乎有所不同(或者我遗漏了一些东西) 下面的示例是一个小型文本编辑器,其中一个打开的对话框按钮被打包到HeaderBar中。但是,clicked.connection语法返回错误 代码如下:Gtk 如何使用工具按钮点击信号(在标题栏中)?,gtk,vala,genie,Gtk,Vala,Genie,创建简单的ToolButton时,可以使用中的clicked.connect界面。将按钮添加到的界面类似于该示例中所示的界面。然而,处理点击连接的方式似乎有所不同(或者我遗漏了一些东西) 下面的示例是一个小型文本编辑器,其中一个打开的对话框按钮被打包到HeaderBar中。但是,clicked.connection语法返回错误 代码如下: [indent=4] uses Gtk init Gtk.init (ref args) var app = new Applic
[indent=4]
uses
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
open_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.OPEN)
open_button.clicked.connect (openfile)
// Add everything to the toolbar
headerbar.pack_start (open_button)
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)
open_button.clicked.connect在编译时返回:
text_editor-exercise_7_1.gs:134.32-134.39: error: Argument 1: Cannot convert from `Application.openfile' to `Gtk.ToolButton.clicked'
open_button.clicked.connect (openfile)
使用HeaderBar小部件时,处理该信号的方式是否会改变
只要对行进行注释,代码就可以工作(您可能需要为openfile函数添加存根)
谢谢
更新
这个问题值得更新,因为错误实际上不在我上面所附的正文中
错误出现在函数的定义处。我写道:
def openfile (self:Button)
当我应该这样做的时候:
def openfile (self:ToolButton)
或者简单地说:
def openfile ()
您的代码中没有包含click处理程序,使用此示例,它可以正常工作:
def openfile ()
warning ("Button clicked")
所以我猜你的点击处理程序的类型签名是错误的,这就是为什么编译器在这里抱怨的原因