1

当我将 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 动作的行为不同?
4

2 回答 2

1

默认情况下,stdout 是行缓冲的。

对于输出,您可以执行以下操作:

var that = "message"
print that
message (that)


stdout.printf ("%s\n", that)    // add a newline
stdout.printf (that)   // no newline, but
stdout.flush ()             // flush if no newline in that

这里有一个问题:

var _entry = ...    // this one unvisiable in file_changed
_entry = ...        // remove var, it will visiable in file_changed

但我的代码仍然有问题(在我的机器上):

不支持操作

try
    info:FileInfo = file.query_info (FileAttribute.ACCESS_CAN_WRITE, FileQueryInfoFlags.NONE, null)
    writable: bool = info.get_attribute_boolean (FileAttribute.ACCESS_CAN_WRITE)
    if writable is true
    _entry.set_sensitive (false)

except e: Error
    print e.message // opration not supported

我试图找出问题所在。但一无所获。

于 2016-01-16T09:41:21.767 回答
0

感谢 Zee 和 andlabs。答案包括两个建议。

首先,正确的信号是文件集,而不是选择改变。其次,get_filename 捕获了错误的 uri 格式。我应该改用 get_uri 。这就是操作不支持错误的原因。

另外,感谢您让我知道 message () 函数,非常有用。

最后,该函数应类似于:

    def file_changed ( file_chooser_widget: FileChooser )
    file:File = File.new_for_uri(file_chooser_widget.get_uri())
    try
        info:FileInfo = file.query_info (FileAttribute.ACCESS_CAN_WRITE, FileQueryInfoFlags.NONE, null)
        message ("after info")
        writable: bool = info.get_attribute_boolean (FileAttribute.ACCESS_CAN_WRITE)
        if writable is false
            _entry.set_sensitive (false)
    except e: Error
        print e.message
于 2016-01-16T12:34:02.447 回答