简体   繁体   English

tkinter-如何打开多个文件对话框?

[英]tkinter - how to open multiple filedialogs?

in python's default editor, IDLE, it is possible to have multiple 'Open' dialogs opened at the same time. 在python的默认编辑器IDLE中,可以同时打开多个“打开”对话框。

I'm looking at their source, but I can't find where I can have this behavior replicated. 我正在查看其来源,但找不到在哪里可以复制此行为。 from their IOBinding.py 's : 从他们的IOBinding.py的:

from tkinter import filedialog as TkFileDialog
...

class IOBinding:
    ...
    def askopenfile(self):
        dir, base = self.defaultfilename("open")
        if not self.opendialog:
            self.opendialog = tkFileDialog.Open(master=self.text,
                                                filetypes=self.filetypes)
        filename = self.opendialog.show(initialdir=dir, initialfile=base)
        return filename

so they do use tkinter's built-in filedialog module, but I can't find way to have some 'modeless' dialogs. 因此他们确实使用了tkinter的内置filedialog模块,但是我找不到找到“无模式”对话框的方法。 I can have dialogs opened by two codes, which are basically same: 我可以用两个基本相同的代码打开对话框:

from tkinter import filedialog as tkFileDialog

file_name = tkFileDialog.Open( ... ).show()
file_name = tkFileDialog.askopenfilename()

but they blocks whole application - users cannot switch windows or issue new command until they close the dialog. 但是它们阻止了整个应用程序-用户只有在关闭对话框后才能切换窗口或发出新命令。 also, I can't call these dialog functions from different thread - that will kill whole my Tk app. 同样,我不能从不同的线程调用这些对话框函数-这将杀死我的整个Tk应用程序。 What should I do? 我该怎么办?

filedialog have parent option. filedialogparent选项。 You can change it to hidden window to prevent blocking the root window: 您可以将其更改为隐藏窗口,以防止阻塞根窗口:

from tkinter import filedialog as tkFileDialog
from tkinter import *

def ask_open():
    p = hidden if attach_to_hidden.get() else root
    tkFileDialog.Open(parent=p).show()

root = Tk()
hidden = Toplevel()
hidden.withdraw()

attach_to_hidden = IntVar()
Checkbutton(root, text='Attach to hidden window', variable=attach_to_hidden).pack()
Button(root, text='Open', command=ask_open).pack()

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM