FileChooserButton 行为
FileChooserButton behaviour
当我将 selection_changed 连接到
FileChooserButton 中的函数。在...的帮助下
我现在理解 selection_changed 语法的用户。但奇怪的是
当我在同一个程序中第二次使用它时发生。
上下文
我的目标是创建一个带有两个 FileChooserButtons 和一个 Entry 文本的 window
在 window 的末尾位置。第一个 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 行动?
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
我已经尝试找出问题所在。但一无所获
感谢 Zee 和 andlabs。答案包括这两个建议。
首先,正确的信号是file-set,而不是改变选择。其次,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
当我将 selection_changed 连接到
FileChooserButton 中的函数。在...的帮助下
上下文
我的目标是创建一个带有两个 FileChooserButtons 和一个 Entry 文本的 window 在 window 的末尾位置。第一个 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 行动?
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
我已经尝试找出问题所在。但一无所获
感谢 Zee 和 andlabs。答案包括这两个建议。
首先,正确的信号是file-set,而不是改变选择。其次,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