小编Nei*_*nro的帖子

如何在 Gtk3 中实现退出对话框?

我几乎完成了我的 Ubuntu App Showdown 应用程序,但想让它更加健壮,在应用程序退出时,我遍历打开的文件,检查未保存的文件,如果发现任何文件,我会弹出一个对话框通知用户。

我想要发生的是,如果用户取消对话框,程序将恢复,但是如果用户单击确定,对话框和主窗口都应该关闭。

这是我到目前为止。

self.connect("delete-event", self.CheckSave)

def CheckSave(self, arg1, arg2):
    unsaved = False
    for doc in self.Documents:
        if doc.Saved == False:
            unsaved = True

    if unsaved:
        print("Unsaved document")
        Dialog = Gtk.Dialog("Really quit?", 0, Gtk.DialogFlags.MODAL)
        Dialog.add_button(Gtk.STOCK_NO, Gtk.ResponseType.CANCEL)
        Dialog.add_button(Gtk.STOCK_YES, Gtk.ResponseType.OK)

        box = Dialog.get_content_area()
        label1 = Gtk.Label("There are unsaved file(s).")
        label2 = Gtk.Label("Are you sure you want to quit?")
        box.add(label1)
        box.add(label2)
        box.show_all()

        response = Dialog.run()
        print(response)

        if response == Gtk.ResponseType.OK:
            return(False)
        else:
            return(True)

        Dialog.destroy()
Run Code Online (Sandbox Code Playgroud)

当对话框运行时,它从不输出 ResponseType.OK 或 ResponseType.CANCEL 值,我得到随机负数,如 -4 …

python quickly application-development gtk3

3
推荐指数
1
解决办法
4252
查看次数

标签 统计

application-development ×1

gtk3 ×1

python ×1

quickly ×1