Python pyGTK/GTK filechooser-在将文件或文件夹写入光盘之前,是否可以验证用户输入?
创建新文件夹时,我想在gtk.FileChooser对话框中验证用户对文件夹名称的选择。Python pyGTK/GTK filechooser-在将文件或文件夹写入光盘之前,是否可以验证用户输入?,python,gtk,pygtk,filechooser,Python,Gtk,Pygtk,Filechooser,创建新文件夹时,我想在gtk.FileChooser对话框中验证用户对文件夹名称的选择。 我尝试连接到对话“响应”信号,但已经太晚了,新文件夹将写入光盘。 是否有一种方法可以实现这一点,然后在将文件夹写入光盘后对其进行验证 感谢马克。。。以下是我正在使用的代码: import gtk def _newFolderDialog(currentFolder=None): newDialog = gtk.FileChooserDialog( title="Create ne
我尝试连接到对话“响应”信号,但已经太晚了,新文件夹将写入光盘。
是否有一种方法可以实现这一点,然后在将文件夹写入光盘后对其进行验证 感谢马克。。。以下是我正在使用的代码:
import gtk
def _newFolderDialog(currentFolder=None):
newDialog = gtk.FileChooserDialog(
title="Create new folder", parent=None,
action=gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER,
buttons= (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL), backend=None)
newButton = newDialog.add_button(gtk.STOCK_NEW, gtk.RESPONSE_OK)
if currentFolder is not None:
newDialog.set_current_folder(currentFolder)
newButton.connect("pressed", validateChoice, newDialog)
response = newDialog.run()
if response == gtk.RESPONSE_OK:
newFolder = newDialog.get_filename()
newDialog.destroy()
return newFolder
elif response == 1: # Validation was not satisfied
msg = gtk.MessageDialog(parent=None, flags=gtk.DIALOG_MODAL,
type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK,
message_format="Try again!")
msg.run()
msg.destroy()
current_folder = newDialog.get_current_folder()
newDialog.destroy()
return _newFolderDialog(current_folder)
elif response == 2: # Ok button was pressed, but with no entry
current_folder = newDialog.get_current_folder()
newDialog.destroy()
return _newFolderDialog(current_folder)
elif response == gtk.RESPONSE_CANCEL:
newDialog.destroy()
return None
def validateChoice(button, dialog):
newFolder = dialog.get_filename()
if newFolder is None: # The cursor was in the empty text entry box
dialog.response(2)
else:
# If cursor is selecting a folder, lets unselect it, we are intereste
# in the value in the entry text box.
dialog.unselect_filename(newFolder)
newFolder = dialog.get_filename()
if newFolder is None: # A folder was selected but empty text entry box
dialog.response(2)
## do some validation, in this case the folder has to start with "/home"
elif not newFolder.startswith("/home"):
dialog.response(1)
else:
dialog.response(gtk.RESPONSE_OK)
newFolder = _newFolderDialog()
print newFolder
我可以看到两种方法 首先,只需编写自己的响应id处理程序,而不是使用gtk.response_OK(对于小部件来说,这意味着创建文件夹)。如果这样做,您将负责在验证后实际创建文件夹(os.path.mkdir) 第二,您可以覆盖单击“新建”按钮:
谢谢经过几个小时的调整,考虑到不同可能的用户操作,我相信我已经做到了。有问题的新代码发布。
import gtk
def new_button_pressed(widget, data=None):
## data is a reference to the dialog widget
## do some validation, in this case the folder has to start with "/home"
if not(data.get_filename().startswith("/home")):
## force cancel response
print "failed validation..."
data.response(gtk.RESPONSE_CANCEL)
else:
print "success validation..."
data.response(gtk.RESPONSE_OK)
newDialog = gtk.FileChooserDialog(
title="Create new folder", parent=None,
action=gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER,
buttons= (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL), backend=None)
new_button = newDialog.add_button(gtk.STOCK_NEW, gtk.RESPONSE_OK)
new_button.connect("pressed", new_button_pressed, newDialog)
newDialog.run()