我在Python Gtk中有一个应用程序。我的主文件中有我的主Application类。然后,我所有的对话框都放在不同的文件中。我需要能够将自定义数据从对话框传递/返回到标准Gtk响应代码以外的主应用程序类,这是一些基本示例代码,因为我自己的代码很长:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
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")
dialog.destroy()
win = DialogWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
对话框窗口位于单独的文件中:
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))
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()
作为标准,我们将Gtk.ResponseType
应用于按钮。但是,如果我们想返回一些自定义数据-不仅仅是简单的响应代码-作为进一步的代码示例,该怎么办:
class DialogExample(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "My Dialog", parent, 0)
self.set_default_size(150, 100)
label = Gtk.Label("This is a dialog to display additional information")
button = Gtk.Button("Return something")
button.connect("clicked", self.on_button_clicked)
box = self.get_content_area()
box.add(label)
self.show_all()
def on_button_clicked(self, widget):
if SOME_CONDITION:
return <CUSTOM_RESPONSE>
else:
return <ALT_CUSTOM_RESPONSE>
当我做最后一个示例时,对话框不返回任何内容,我想做类似的事情:
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 == <CUSTOM_RESPONSE>:
#do something with <CUSTOM_RESPONSE>
elif response == <ALT_CUSTOM_RESPONSE>:
#do something different with <ALT_CUSTOM_RESPONSE>
dialog.destroy()
win = DialogWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
DialogExample窗口不会销毁/关闭并且什么也不会返回,并且应用程序基本上只是暂停,因为它认为没有更多的方法可以运行-尽管在返回自定义数据之后还有很多事情要做(我需要然后开始将记录添加到数据库中。
[更新]
我现在已经尝试了很多解决该问题的方法,因此无法在此处列出所有内容。我无休止地在寻找某种答案,似乎这不是互联网上任何人都能完成的事情。
答案 0 :(得分:1)
gtk_dialog_run
的C版本仅限于返回整数,您可以设置自定义值,但不能设置字符串或对象。您可以通过在“响应”信号上设置一个值来解决此问题,然后在运行函数返回后再获取它。
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))
self.result = ""
self.set_default_size(150, 100)
self.connect("response", self.on_response)
label = Gtk.Label(label="Type something")
self.entry = Gtk.Entry()
box = self.get_content_area()
box.add(label)
box.add(self.entry)
self.show_all()
def on_response(self, widget, response_id):
self.result = self.entry.get_text ()
def get_result(self):
return self.result
class DialogWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Dialog Example")
self.set_border_width(6)
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(box)
button = Gtk.Button(label="Open dialog")
button.connect("clicked", self.on_button_clicked)
box.add(button)
self.label = Gtk.Label()
box.add(self.label)
def on_button_clicked(self, widget):
dialog = DialogExample(self)
response = dialog.run()
if response == Gtk.ResponseType.OK:
self.label.set_text(dialog.get_result())
print("The OK button was clicked")
elif response == Gtk.ResponseType.CANCEL:
print("The Cancel button was clicked")
dialog.destroy()
win = DialogWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()