【问题标题】:How to make filename/path from Gtk+ (Python 3.4) FileChooserDialog accessible globally如何使 Gtk+ (Python 3.4) FileChooserDialog 中的文件名/路径可全局访问
【发布时间】:2015-05-01 00:28:53
【问题描述】:

我有一些代码在 Python 3.4 中使用 Gtk+ FileChooserDialog 来允许用户选择文件。

然后,它应该关闭对话框(显然)并继续执行用户选择文件之后的代码。但是,发生的情况是用户选择了他们的文件,代码继续,但对话框并没有像它应该的那样消失。

我之前遇到过这个问题,我们当时找出了它发生的原因并解决了它,但现在它又发生了,虽然我知道是什么原因造成的,但我不知道如何真正解决它。

这是我的代码:

from gi.repository import Gtk

class FileChooser():

    def __init__(self):

        global path

        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):
        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()

# path variable will be used after this point

这里的问题是,由于我不知道的原因,如果我在 FileChooser() 类的 __init__() 函数中有 global path 声明,则对话框不会消失。

如果我删除 global path 声明,对话框就会消失,但稍后我在程序中尝试访问 path 变量时会得到一个 NameError: name 'path' is not defined

我也尝试在程序开始时将 path 设为全局,但我仍然收到 NameError。

我可以做些什么来让这个变量在我的程序中稍后可以访问,同时仍然让对话框消失?

【问题讨论】:

  • 小提示:self.path 必须重命名为 self.fpath 什么的,因为每个 gtk 小部件都已经有了 path 属性。

标签: python gtk gtk3 filechooser


【解决方案1】:

path 变量视为FileChooser() 的实例。它为代表FileChooser() 的对话框访问路径提供了一个逻辑端。

class FileChooser():

    def __init__(self):
        #Stores your path
        self.path = None

        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())
            self.path = dia.get_filename()
        elif response == Gtk.ResponseType.CANCEL:
            print("Cancel clicked")

        dia.destroy()

当你创建对象时。

dialog = FileChooser()

您可以通过以下方式访问它:

dialog.path

【讨论】:

  • facepalm 有时,我可以成为一个真正的天才...感谢您指出对我来说显而易见的事情:) +1/accept :P
  • @RPiAwesomeness 相信我,即使我遇到过这些情况 :)
猜你喜欢
  • 1970-01-01
  • 2010-11-19
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
  • 2021-03-24
相关资源
最近更新 更多