gpt4 book ai didi

Python GTK 文件保存对话框,如何创建 "Close Without Saving"ResponseType

转载 作者:行者123 更新时间:2023-12-01 02:24:02 24 4
gpt4 key购买 nike

有时我们需要更多ResponseType用于我们程序的对话框。例如,当用户关闭文件编辑器时,编辑器会显示对话框让用户选择:“取消”、“关闭而不保存”、“保存”但是“保存”和“关闭而不保存”在 Gtk.ResponseType 中不存在。那么我如何为此创建新的响应类型。这个问题可以用其他方法解决吗?

谢谢。

最佳答案

Gtk.ResponseType 它是一小组预定义值。这些是负值,而正值(包括零)则留给应用程序开发人员根据需要使用它们。来自 documentation :

Predefined values for use as response ids in Gtk.Dialog.add_button(). All predefined values are negative; GTK+ leaves values of 0 or greater for application-defined response ids.

因此,当您向对话框添加按钮时,您可以使用自己的一组响应值,而不是使用预定义的 Gtk.ResponseType。

我们以 this example 为例来自python-gtk3 tutorial并使用我们自己的响应类型值添加第三个选项:

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")
...

您可以创建自己的积极响应值枚举集并根据需要使用它们(包括零)。祝你好运。

编辑:

在glade中,使用按钮时,可以将response_id的值设置为整数。它位于小部件的常规设置选项卡中。

关于Python GTK 文件保存对话框,如何创建 "Close Without Saving"ResponseType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47583495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com