如何在Python 3.4中隐藏Gtk + FileChooserDialog? [英] How to hide a Gtk+ FileChooserDialog in Python 3.4?

查看:109
本文介绍了如何在Python 3.4中隐藏Gtk + FileChooserDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个程序,以便它自己显示一个FileChooserDialog(没有主要的Gtk窗口,只是对话框)。



我遇到的问题即使在用户选择了文件并且程序看起来继续执行之后,对话框也不会消失。



下面是一个展示此问题的片段: p>

  from gi.repository import Gtk 

class FileChooser():

def __init __(self):

全局路径

dia = Gtk.FileChooserDialog(请选择一个文件,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)

dialog = FileChooser()

print(path)

input()
quit()

当程序以退出时,对话框消失code>函数调用。



我也试过 dia.hide(),但那doesn也不会工作 - 在代码继续运行时,对话框仍然可见。



使对话框消失的正确方法是什么?

编辑:我从那以后就知道,在没有父窗口的情况下创建一个Gtk对话框是不鼓励的。但是,我不想处理必须让用户关闭一个没有任何内容的窗口,并简单地将其作为对话框的父窗口。



一种方法来创建一个不可见的父窗口,然后在对话框消失后退出Gtk主循环? 您可以设置一个窗口先做:

  def __init__(self):

[.. snip ..]

w = Gtk.Window()

dia = Gtk.FileChooserDialog(请选择一个文件,w,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL,Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN,Gtk.ResponseType.OK))

也设置路径的默认值以防用户取消:

  path =''

然后,在脚本结尾处:

  print(path)

while Gtk.events_pending():
Gtk.main_iteration ()

print(done)

收集并处理所有活动


I have a program set up so that it displays a FileChooserDialog all by itself (no main Gtk window, just the dialog).

The problem I'm having is that the dialog doesn't disappear, even after the user has selected the file and the program has seemingly continued executing.

Here's a snippet that showcases this issue:

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)

dialog = FileChooser()

print(path)

input()
quit()

The dialog only disappears when the program exits with the quit() function call.

I've also tried dia.hide(), but that doesn't work either - the dialog is still visible while code continues running.

What would the proper way to make the dialog disappear?

EDIT: I've since learned that it's discouraged to make a Gtk dialog without a parent window. However, I don't want to deal with having to have the user close a window that has nothing in it and simply stands as the parent for the dialog.

Is there a way to make an invisible parent window and then quit the Gtk main loop when the dialog disappears?

解决方案

You can set up a window first by doing:

def __init__ (self):

  [.. snip ..]

  w = Gtk.Window ()

  dia = Gtk.FileChooserDialog("Please choose a file", w,
        Gtk.FileChooserAction.OPEN,
        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
         Gtk.STOCK_OPEN, Gtk.ResponseType.OK))

also, set a default value for path in case the user cancels:

  path = ''

Then, at the end of your script:

print (path)

while Gtk.events_pending ():
  Gtk.main_iteration ()

print ("done")

to collect and handle all events.

这篇关于如何在Python 3.4中隐藏Gtk + FileChooserDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆