Python GTK+ 3 只拉出一个文件选择器对话框?
Python GTK+ 3 pull up just a file chooser dialog?
我正在尝试创建一个使用 Gtk FileChooserDialog 对话框的程序。我遵循了教程 here,并且有效。但是,我想 只是 弹出对话框,而不必处理实际的 Gtk window。
我尝试将教程中 on_file_clicked()
函数中的代码放入 __init__
函数中(并稍微调整一下 看起来 就像它可以工作)删除已经在 __init__
:
中的代码
class FileChooserWindow(Gtk.Window):
def __init__(self):
global path
dialog = Gtk.FileChooserDialog("Please choose a file", self,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
self.add_filters(dialog)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected: " + dialog.get_filename())
path = dialog.get_filename()
dialog.destroy()
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
def add_filters(self, dialog):
filter_any = Gtk.FileFilter()
filter_any.set_name("Any files")
filter_any.add_pattern("*")
dialog.add_filter(filter_any)
filter_text = Gtk.FileFilter()
filter_text.set_name('Text files')
filter_text.add_mime_type('text/plain')
dialog.add_filter(filter_text)
filter_py = Gtk.FileFilter()
filter_py.set_name('Python files')
filter_py.add_mime_type('text/x-python')
dialog.add_filter(filter_py)
filter_img = Gtk.FileFilter()
filter_img.set_name('Image')
filter_img.add_mime_type('image/*')
dialog.add_filter(filter_img)
win = FileChooserWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
但只是 returns 这个错误:
Traceback (most recent call last):
File "base.py", line 152, in <module>
win = FileChooserWindow()
File "base.py", line 38, in __init__
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
File "/usr/lib/python3/dist-packages/gi/overrides/__init__.py", line 175, in new_init
return super_init_func(self, **new_kwargs)
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 500, in __init__
self._init(*args, **new_kwargs)
File "/usr/lib/python3/dist-packages/gi/overrides/__init__.py", line 175, in new_init
return super_init_func(self, **new_kwargs)
File "/usr/lib/python3/dist-packages/gi/overrides/__init__.py", line 175, in new_init
return super_init_func(self, **new_kwargs)
TypeError: could not convert value for property `transient_for' from FileChooserWindow to GtkWindow
有谁知道仅 Gtk FileChooserDialog 对话框的正确方法?
当您在第 6 行声明对话框时:
dialog = Gtk.FileChooserDialog("Please choose a file", self,
self
是对话框的父级。要创建一个没有父级的对话框,请将其设置为 None
,如下所示:
dialog = Gtk.FileChooserDialog("Please choose a file", None,
@Seths 上面的解决方案因以下错误而崩溃:
Traceback (most recent call last):
File "scriptname.py", line 57, in <module>
win.connect("delete-event", Gtk.main_quit)
TypeError: object at 0x7ff81d5dedc0 of type FileChooserWindow is not initialized
CRITICAL: Exiting due to uncaught exception <class 'TypeError'>
我找到了一个 simpler PyGObject example 并修复了一些错误以尝试让它工作,但它阻塞在主循环并等待:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
filechooserdialog = Gtk.FileChooserDialog(title="Open...",
parent=None,
action=Gtk.FileChooserAction.OPEN)
filechooserdialog.add_buttons("_Open", Gtk.ResponseType.OK)
filechooserdialog.add_buttons("_Cancel", Gtk.ResponseType.CANCEL)
filechooserdialog.set_default_response(Gtk.ResponseType.OK)
response = filechooserdialog.run()
if response == Gtk.ResponseType.OK:
print("File selected: %s" % filechooserdialog.get_filename())
filechooserdialog.destroy()
我正在尝试创建一个使用 Gtk FileChooserDialog 对话框的程序。我遵循了教程 here,并且有效。但是,我想 只是 弹出对话框,而不必处理实际的 Gtk window。
我尝试将教程中 on_file_clicked()
函数中的代码放入 __init__
函数中(并稍微调整一下 看起来 就像它可以工作)删除已经在 __init__
:
class FileChooserWindow(Gtk.Window):
def __init__(self):
global path
dialog = Gtk.FileChooserDialog("Please choose a file", self,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
self.add_filters(dialog)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected: " + dialog.get_filename())
path = dialog.get_filename()
dialog.destroy()
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
def add_filters(self, dialog):
filter_any = Gtk.FileFilter()
filter_any.set_name("Any files")
filter_any.add_pattern("*")
dialog.add_filter(filter_any)
filter_text = Gtk.FileFilter()
filter_text.set_name('Text files')
filter_text.add_mime_type('text/plain')
dialog.add_filter(filter_text)
filter_py = Gtk.FileFilter()
filter_py.set_name('Python files')
filter_py.add_mime_type('text/x-python')
dialog.add_filter(filter_py)
filter_img = Gtk.FileFilter()
filter_img.set_name('Image')
filter_img.add_mime_type('image/*')
dialog.add_filter(filter_img)
win = FileChooserWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
但只是 returns 这个错误:
Traceback (most recent call last):
File "base.py", line 152, in <module>
win = FileChooserWindow()
File "base.py", line 38, in __init__
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
File "/usr/lib/python3/dist-packages/gi/overrides/__init__.py", line 175, in new_init
return super_init_func(self, **new_kwargs)
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 500, in __init__
self._init(*args, **new_kwargs)
File "/usr/lib/python3/dist-packages/gi/overrides/__init__.py", line 175, in new_init
return super_init_func(self, **new_kwargs)
File "/usr/lib/python3/dist-packages/gi/overrides/__init__.py", line 175, in new_init
return super_init_func(self, **new_kwargs)
TypeError: could not convert value for property `transient_for' from FileChooserWindow to GtkWindow
有谁知道仅 Gtk FileChooserDialog 对话框的正确方法?
当您在第 6 行声明对话框时:
dialog = Gtk.FileChooserDialog("Please choose a file", self,
self
是对话框的父级。要创建一个没有父级的对话框,请将其设置为 None
,如下所示:
dialog = Gtk.FileChooserDialog("Please choose a file", None,
@Seths 上面的解决方案因以下错误而崩溃:
Traceback (most recent call last):
File "scriptname.py", line 57, in <module>
win.connect("delete-event", Gtk.main_quit)
TypeError: object at 0x7ff81d5dedc0 of type FileChooserWindow is not initialized
CRITICAL: Exiting due to uncaught exception <class 'TypeError'>
我找到了一个 simpler PyGObject example 并修复了一些错误以尝试让它工作,但它阻塞在主循环并等待:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
filechooserdialog = Gtk.FileChooserDialog(title="Open...",
parent=None,
action=Gtk.FileChooserAction.OPEN)
filechooserdialog.add_buttons("_Open", Gtk.ResponseType.OK)
filechooserdialog.add_buttons("_Cancel", Gtk.ResponseType.CANCEL)
filechooserdialog.set_default_response(Gtk.ResponseType.OK)
response = filechooserdialog.run()
if response == Gtk.ResponseType.OK:
print("File selected: %s" % filechooserdialog.get_filename())
filechooserdialog.destroy()