[英]Python GTK File Save Dialog, How Can I create “Close Without Saving” ResponseType
有时,当我们需要更多的ResponseType
用于程序对话框时。 例如,当用户关闭文件编辑器时,编辑器将显示对话框以获取用户选择:“取消”,“不保存就关闭”“保存”,但是Gtk.ResponseType
中不存在“保存”和“不保存就关闭”我如何为此创建新的响应类型。 可以用另一种方式解决此问题吗?
谢谢。
Gtk.ResponseType
小的预定义值。 这些是负值,而正值(包括零)留给应用程序开发人员根据需要使用它们。 从文档中 :
在Gtk.Dialog.add_button()中用作响应ID的预定义值。 所有预定义值均为负; GTK +为应用程序定义的响应ID保留0或更大的值。
因此,当您向对话框中添加按钮时,可以使用自己的一组响应值来代替使用预定义的Gtk.ResponseType。
让我们以python-gtk3教程中的 示例为例 ,并使用我们自己的响应类型值添加第三个选项:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class DialogExample(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK, "OPTION 3", 1))
self.set_default_size(150, 100)
label = Gtk.Label("This is a dialog to display additional information")
box = self.get_content_area()
box.add(label)
self.show_all()
class DialogWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Dialog Example")
self.set_border_width(6)
button = Gtk.Button("Open dialog")
button.connect("clicked", self.on_button_clicked)
self.add(button)
def on_button_clicked(self, widget):
dialog = DialogExample(self)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("The OK button was clicked")
elif response == Gtk.ResponseType.CANCEL:
print("The Cancel button was clicked")
elif response == 1:
print("OPTION 3 was clicked")
dialog.destroy()
win = DialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
做了什么?
我们添加了带有标签OPTION 3的第三个按钮,其响应ID值为1
:
Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK, "OPTION 3", 1))
然后,在处理响应时,我们可以检查该响应ID值并执行其他操作:
...
print("The Cancel button was clicked")
elif response == 1:
print("OPTION 3 was clicked")
...
您可以创建自己的一组正响应值,并根据需要使用它们(包括零)。 祝好运。
编辑 :
在林间空地中,使用按钮时,可以将response_id
的值设置为整数。 它位于小部件的“常规设置”选项卡中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.