【问题标题】:FileChooserButton behaviourFileChooserButton 行为
【发布时间】:2016-01-16 00:37:14
【问题描述】:

当我将 selection_changed 连接到 FileChooserButton 中的函数。在...的帮助下 SO 用户我现在了解 selection_changed 语法。但奇怪的是 当我在同一个程序中第二次使用它时发生。

上下文

我的目标是创建一个带有两个 FileChooserButtons 和一个 Entry 文本的窗口 在窗口的结束位置。第一个 FileChooserButton 帮助 用户选择目录并导致第二个 FileChooserButton 打开 用户在第一个目录中选择的目录。到目前为止,代码 工作正常。该条目也被正确绘制并显示“Here go 文件名”。预期的行为是在 检查后在第二个 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 与带有 Action.OPEN 的行为不同 SELECT_FOLDER 操作?

【问题讨论】:

  • 您希望何时发送信号,当用户单击文件列表中的某个项目但仍在按钮打开的对话框中工作时,或者当用户单击按钮时关闭选择器对话框?
  • 当他点击按钮关闭它时。
  • 那么你不想使用selection-changed信号;那是在更改列表中的选择时。相反,您需要file-set 信号(特定于 GtkFileChooserButton)。

标签: gtk genie


【解决方案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

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

【讨论】:

    【解决方案2】:

    感谢 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
    

    【讨论】:

    • 嗨!路易斯?你有什么错误吗?因为,我对此有一些错误。这对我来说是个大问题?!
    • 我有一些未处理的错误,因为代码还没有完成。你可以在这个pastebin找到整个代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多