这是练习的一部分。在SO,我找不到类似的问题。这是一个小GTK程序,在单击重命名按钮后,应重命名该文件。
虽然uri似乎指向正确的文件,但由于某种原因,我在运行时得到断言G_IS_FILE失败。
以下是代码:
[indent=4]
uses
Gtk
class TestWindow:Window
_file_chooser:FileChooserButton
_entry:Gtk.Entry
_button:Gtk.Button
_file:File
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() )
//I used selection_changed directly as per the question in stack_exchange
//http://stackoverflow.com/questions/34689763/the-signal-connect-syntax
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.file_set.connect( file_changed )
_entry = new Gtk.Entry()
_entry.set_text("Here the file name")
//_entry.activate.connect(name_altered)
_button = new Button.with_label("Rename")
_button.set_sensitive(false)
_button.clicked.connect(btn_pressed)
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 )
box.pack_start( _button, 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 )
_file:File = File.new_for_uri(file_chooser_widget.get_uri())
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
_entry.set_sensitive (false)
else
_button.set_sensitive (true)
except e: Error
print e.message
_entry.set_text(_file.get_path())
def btn_pressed ()
_file.set_display_name(_entry.get_text())
init
Gtk.init( ref args )
var test = new TestWindow()
test.show_all()
Gtk.main()
问题出现在以下行:_file.set_display_name(_entry.get_text())
。我正在寻找一些关于我在这个小精灵代码中做错了什么的见解。
答案 0 :(得分:2)
_file
时, _file.set_display_name()
为空。问题出在这里:
_file:File = File.new_for_uri(file_chooser_widget.get_uri())
您正在创建一个名为_file
的新变量,该变量会隐藏_file
字段。将其更改为
_file = File.new_for_uri(file_chooser_widget.get_uri())
应该解决问题。请注意,您也可以使用this._field
来访问该字段;输入时间要长一点,但更容易阅读,这样会使错误变得更加明显。