当我将 selection_changed 连接到 FileChooserButton 中的函数时,我遇到了一个奇怪的行为。在SO用户的帮助下, 我现在了解了 selection_changed 语法。但是当我在同一个程序中第二次使用它时,发生了一些奇怪的事情。
语境
我的目标是创建一个窗口,其中包含两个 FileChooserButtons 和一个位于窗口末尾位置的 Entry 文本。第一个 FileChooserButton 帮助用户选择目录并使第二个 FileChooserButton 在用户在第一个中选择的目录中打开。到目前为止,代码运行良好。该条目也被正确绘制并显示“文件名在这里”。预期的行为是在检查文件是否可写后,在第二个 FileChooserButton 中选择文件名时更改条目的文本。
我使用的策略是将 selection_changed 连接到一个函数,该函数负责检查文件是否可写以及条目文本的更改。
问题是该函数从未被调用。我添加了一个调试愚蠢的代码,如:
stdout.printf("Checking whether this function is actually called")
它永远不会被打印出来,因此我想这个函数永远不会被调用。有问题的函数是下面的 file_changed 函数。
[indent=4]
uses
Gtk
class TestWindow:Window
_file_chooser:FileChooserButton
_entry:Gtk.Entry
construct()
title = "File chooser"
window_position = WindowPosition.CENTER
destroy.connect( Gtk.main_quit )
var folder_chooser = new FileChooserButton("Choose a Folder",FileChooserAction.SELECT_FOLDER)
folder_chooser.set_current_folder( Environment.get_home_dir() )
folder_chooser.selection_changed.connect( folder_changed )
_file_chooser = new FileChooserButton("Choose a File",FileChooserAction.OPEN)
_file_chooser.set_current_folder( Environment.get_home_dir() )
_file_chooser.selection_changed.connect( file_changed )
var _entry = new Gtk.Entry()
_entry.set_text("Here the file name")
var box = new Box( Orientation.VERTICAL, 0 )
box.pack_start( folder_chooser, true, true, 0 )
box.pack_start( _file_chooser, true, true, 0 )
box.pack_start( _entry, true, true, 0 )
add( box )
def folder_changed( folder_chooser_widget:FileChooser )
folder:string = folder_chooser_widget.get_uri()
_file_chooser.set_current_folder_uri( folder )
def file_changed ( file_chooser_widget: FileChooser )
stdout.printf(file_chooser_widget.get_filename())
stdout.printf("Checking whether this function is actually called")
file:File = File.new_for_uri(file_chooser_widget.get_filename())
stdout.printf(file_chooser_widget.get_filename())
info:FileInfo = file.query_info (FileAttribute.ACCESS_CAN_WRITE, FileQueryInfoFlags.NONE, null)
writable:bool = info.get_attribute_boolean (FileAttribute.ACCESS_CAN_WRITE)
stdout.printf(writable.to_string())
if writable is true
_entry.set_sensitive(false)
init
Gtk.init( ref args )
var test = new TestWindow()
test.show_all()
Gtk.main()
问题
- 为什么第二个 selection_changed 调用不起作用?带有 Action.OPEN 的 FileChooserButton 的行为是否与带有 SELECT_FOLDER 动作的行为不同?