Python 脚本中的 Gtk.main_quit() 实际上并没有退出 Gtk 主循环
Gtk.main_quit() in Python script doesn't actually quit the Gtk main loop
我有一个项目使用 Gtk.FileChooserDialog 对象,显然,允许用户选择文件。然后,选择文件后,对话框应该消失,代码应该继续 运行.
虽然对话框 消失了,但代码不会从 Gtk.main()
之后继续 - 我的代码似乎卡在了 Gtk 主循环中。
代码如下:
from gi.repository import Gtk
class FileChooser(Gtk.FileChooserDialog):
def __init__(self):
dia = Gtk.FileChooserDialog("Please choose a file", None,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
self.add_filters(dia)
response = dia.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected: " + dia.get_filename())
path = dia.get_filename()
dia.destroy()
Gtk.main_quit() # <- Doesn't work?
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dia.destroy()
Gtk.main_quit() # <- Doesn't work?
def add_filters(self, dia):
filter_any = Gtk.FileFilter()
filter_any.set_name("Any files")
filter_any.add_pattern("*")
dia.add_filter(filter_any)
filter_text = Gtk.FileFilter()
filter_text.set_name('Text files')
filter_text.add_mime_type('text/plain')
dia.add_filter(filter_text)
filter_py = Gtk.FileFilter()
filter_py.set_name('Python files')
filter_py.add_mime_type('text/x-python')
dia.add_filter(filter_py)
filter_img = Gtk.FileFilter()
filter_img.set_name('Image')
filter_img.add_mime_type('image/*')
dia.add_filter(filter_img)
dialog = FileChooser()
dialog.show_all()
Gtk.main()
我也曾尝试在创建 dia
对象后立即添加 dialog.connect('destroy', Gtk.main_quit)
,但这也不起作用。
是否存在一些明显的错误,以至于调用 Gtk.main_quit()
不会使我的代码退出 Gtk 主循环?
如果您只使用 FileChooserDialog,则不必执行 Gtk.main()
或 Gtk.main_quit()
。而且,继承自Gtk.FileChooserDialog也没什么用,可以去掉。
这应该有效:
from gi.repository import Gtk
class FileChooser():
def __init__(self):
dia = Gtk.FileChooserDialog("Please choose a file", None,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
self.add_filters(dia)
response = dia.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected: " + dia.get_filename())
path = dia.get_filename()
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dia.destroy()
def add_filters(self, dia):
pass # filters left out to make the snippet shorter
dialog = FileChooser()
我有一个项目使用 Gtk.FileChooserDialog 对象,显然,允许用户选择文件。然后,选择文件后,对话框应该消失,代码应该继续 运行.
虽然对话框 消失了,但代码不会从 Gtk.main()
之后继续 - 我的代码似乎卡在了 Gtk 主循环中。
代码如下:
from gi.repository import Gtk
class FileChooser(Gtk.FileChooserDialog):
def __init__(self):
dia = Gtk.FileChooserDialog("Please choose a file", None,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
self.add_filters(dia)
response = dia.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected: " + dia.get_filename())
path = dia.get_filename()
dia.destroy()
Gtk.main_quit() # <- Doesn't work?
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dia.destroy()
Gtk.main_quit() # <- Doesn't work?
def add_filters(self, dia):
filter_any = Gtk.FileFilter()
filter_any.set_name("Any files")
filter_any.add_pattern("*")
dia.add_filter(filter_any)
filter_text = Gtk.FileFilter()
filter_text.set_name('Text files')
filter_text.add_mime_type('text/plain')
dia.add_filter(filter_text)
filter_py = Gtk.FileFilter()
filter_py.set_name('Python files')
filter_py.add_mime_type('text/x-python')
dia.add_filter(filter_py)
filter_img = Gtk.FileFilter()
filter_img.set_name('Image')
filter_img.add_mime_type('image/*')
dia.add_filter(filter_img)
dialog = FileChooser()
dialog.show_all()
Gtk.main()
我也曾尝试在创建 dia
对象后立即添加 dialog.connect('destroy', Gtk.main_quit)
,但这也不起作用。
是否存在一些明显的错误,以至于调用 Gtk.main_quit()
不会使我的代码退出 Gtk 主循环?
如果您只使用 FileChooserDialog,则不必执行 Gtk.main()
或 Gtk.main_quit()
。而且,继承自Gtk.FileChooserDialog也没什么用,可以去掉。
这应该有效:
from gi.repository import Gtk
class FileChooser():
def __init__(self):
dia = Gtk.FileChooserDialog("Please choose a file", None,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
self.add_filters(dia)
response = dia.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected: " + dia.get_filename())
path = dia.get_filename()
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dia.destroy()
def add_filters(self, dia):
pass # filters left out to make the snippet shorter
dialog = FileChooser()