Python GTK:确认覆盖对话框阻止filechooser对话框

时间:2015-01-21 19:59:48

标签: python gtk filechooser

我有一个Gtk.Button打开一个Gtk.FileChooserDialog来保存文件。我实现了一个Gtk.Dialog进行确认,当所选文件名已经存在于目标文件夹中时,弹出该文件以保存文件。如果我点击'取消'在此对话框中,确认对话框已销毁,但我无法使用'取消'或者'保存' Gtk.FileChooserDialog的按钮了。 任何帮助表示赞赏。感谢。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
from gi.repository import Gtk

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="demo")
        self.set_position(Gtk.WindowPosition.CENTER)
        self.button = Gtk.Button()
        self.button.set_image(Gtk.Image(stock=Gtk.STOCK_SAVE))
        self.button.connect('clicked', self.on_button_clicked)
        self.add(self.button)

    def on_button_clicked(self, widget):
        dialog = Gtk.FileChooserDialog("Save file", self,
            Gtk.FileChooserAction.SAVE,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
        response = dialog.run()
        if response == Gtk.ResponseType.OK:  # OK button was pressed or existing file was double clicked
            cansave = False
            if os.path.exists(dialog.get_filename()) == True:  # does file already exists?
                dialog2 = DialogSaveFile(self, dialog.get_filename())  # ask to confirm overwrite
                response = dialog2.run()
                if response == Gtk.ResponseType.OK:
                    cansave = True
                else:
                    pass
                dialog2.destroy()
            else:
                cansave = True
            if cansave == True:  # save new file
                open(dialog.get_filename(), "w").close
                dialog.destroy()
            else:
                pass
        else:
            dialog.destroy()

class DialogSaveFile(Gtk.Dialog):
    def __init__(self, parent, db):
        Gtk.Dialog.__init__(self, "Confirm overwrite", parent, 0,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OK, Gtk.ResponseType.OK))
        self.box = self.get_content_area()
        self.label = Gtk.Label("The file `" + db + "` exists.\nDo you want it to be overwritten?")
        self.box.add(self.label)
        self.show_all()

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

2 个答案:

答案 0 :(得分:2)

一旦您离开response = dialog.run()运行循环,您需要重新创建文件对话框,或再次调用dialog.run()以将文件对话框重新置于运行循环中,以便您可以找到按下了什么按钮。

重构它以便文件对话框处理程序在一个单独的函数中应该做的伎俩(没有经过测试,但你会得到这个想法)

def on_button_clicked(self, widget):
    dialog = Gtk.FileChooserDialog("Save file", self,
        Gtk.FileChooserAction.SAVE,
        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
         Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
    self.handle_file_dialog(dialog)

def handle_file_dialog(self, dialog):
    response = dialog.run()
    if response == Gtk.ResponseType.OK:  # OK button was pressed or existing file was double clicked
        cansave = False
        if os.path.exists(dialog.get_filename()) == True:  # does file already exists?
            dialog2 = DialogSaveFile(self, dialog.get_filename())  # ask to confirm overwrite
            response = dialog2.run()
            if response == Gtk.ResponseType.OK:
                cansave = True
                dialog2.destroy()
            else:
                dialog2.destroy()
                # We need to re-run the file dialog to detect the buttons
                self.handle_file_dialog(dialog)
                return
        else:
            cansave = True
        if cansave == True:  # save new file
            open(dialog.get_filename(), "w").close
            dialog.destroy()
        else:
            pass
    else:
        dialog.destroy()

答案 1 :(得分:0)

您可以使用: dialog.set_do_overwrite_confirmation(正确) 如果用户键入已经存在的文件名,则此模式将显示一个确认对话框!


import os
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="demo")
        self.set_position(Gtk.WindowPosition.CENTER)
        self.button = Gtk.Button()
        self.button.set_image(Gtk.Image(stock=Gtk.STOCK_SAVE))
        self.button.connect('clicked', self.on_button_clicked)
        self.add(self.button)

    def on_button_clicked(self, widget):
        dialog = Gtk.FileChooserDialog(parent=self,title="Save file",
            action=Gtk.FileChooserAction.SAVE)
        dialog.add_buttons(Gtk.STOCK_CANCEL,Gtk.ResponseType.CANCEL)
        dialog.add_buttons(Gtk.STOCK_OK,Gtk.ResponseType.OK)
        
        dialog.set_do_overwrite_confirmation(True)
        # This mode will present a confirmation dialog if the user types a file name that already exists.
        response = dialog.run()
        print(response)
        
        if response == Gtk.ResponseType.OK:  # save new file
                open(dialog.get_filename(), "w").close
                dialog.destroy()
        if response == Gtk.ResponseType.CANCEL:  
                dialog.destroy()
        dialog.destroy()

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()