我想使用对话框和消息框,但不想打开tkinter窗口。
有人能教我如何禁用窗口,或如何重新创建窗口,或者向我展示一个不同的解决方案,以便我可以做这些事情。
所有的消息框都是相同的,我想尝试最多的是filedialog.askopenfile、filedialog.askdirectory和filedialog.asksaveasfilename。
我想使用对话框和消息框,但不想打开tkinter窗口。
有人能教我如何禁用窗口,或如何重新创建窗口,或者向我展示一个不同的解决方案,以便我可以做这些事情。
所有的消息框都是相同的,我想尝试最多的是filedialog.askopenfile、filedialog.askdirectory和filedialog.asksaveasfilename。
像这样吗?
from Tkinter import *
import tkFileDialog
import tkMessageBox
Tk().withdraw()
tkFileDialog.askopenfilename()
tkMessageBox.showerror("message", "words")
.withdraw()
的一个问题。.withdraw()
时,tkinter文件对话框也会从任务栏中消失。这意味着当文件对话框失去焦点(比如当你点击另一个(在我这里是pygame)窗口与之交互时),文件对话框会消失,并且无法通过点击任务栏中的图标重新打开,但它仍然在后台活动,这会永久暂停我的pygame应用程序(“软锁定”用户)。iconify()
而不是withdraw()
,正如其名称所示,它不会从任务栏中移除图标(允许用户重新打开窗口!),但会最小化窗口。import tkinter
from tkinter import filedialog
root = tkinter.Tk() # } execute these lines once, to "initialise" tkinter
root.iconify() # }
def prompt_files():
try:
file_paths = filedialog.askopenfilenames()
root.iconify()
except tkinter.TclError: # this error was raised once or twice while testing, just in case return an empty list
return []
else:
return file_paths
def stop_tk(): # call this when you're done selecting files, to stop tkinter from running mainloop in the background
root.destroy()