3

新しいフォルダーを作成するときに、gtk.FileChooser ダイアログでユーザーが選択したフォルダー名を検証したいと思います。
ダイアログの「応答」信号に接続しようとしましたが、既に手遅れです。新しいフォルダーがディスクに書き込まれます。
これを達成し、ディスクに書き込まれた後にフォルダを検証する方法はありますか?

マークのおかげで...私が使用しているコードは次のとおりです。

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
4

1 に答える 1

3

これを行うにはいくつかの方法があります。

1 つ目は、gtk.RESPONSE_OK を使用する代わりに、独自の応答 ID ハンドラーを単純に作成することです (これは、ウィジェットにとってフォルダーの作成を意味します)。このようにすると、検証後に実際にフォルダーを作成する責任があります (os.path.mkdir)。

2 つ目は、「新規」ボタンのクリックをオーバーライドできます。

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()
于 2010-12-23T16:40:09.877 に答える