Flo*_*ger 5 python macos filedialog tkinter matplotlib
编辑:这似乎是 Mac OS 系统上仅限于 Tcl/Tk 的问题。因此,如果您没有这方面的经验,这个话题可能没有意义......
我想要一个可以做两件事的 Python 脚本:
问题是,matplotlib 使用 Tkinter 进行图形表示,每当我pyplot.show()以非交互模式调用时,(关闭之前的)文件对话框会再次弹出。在我看来,它pyplot.show()收集了所有 Tkinter 窗口的列表并显示了所有窗口。然而我没有找到任何帮助。我尝试了Python 2.7和3.3,因为很多Tkinter模块似乎已经改变,但这是相同的现象。我想出的稍微奇怪的解决方法是进入 matplotlib 交互模式,然后使用命令保持窗口打开raw_input()。
以下是在 Python 2 和 3 中运行的最小代码片段,用于显示该问题:
import matplotlib.pyplot as plt
# import Tkinter GUI (changes from Python 2.x to 3.x)
try:
import Tkinter
except (ImportError):
import tkinter as Tkinter
try:
from tkFileDialog import askopenfilename
except (ImportError):
from tkinter.filedialog import askopenfilename
root = Tkinter.Tk()
root.withdraw()
input_filename = askopenfilename(master=root)
# This seemed promising, but it doesn't help
root.destroy()
plt.figure()
# uncommenting this to switch to interactive mode is a workaround
#plt.ion()
plt.show()
# Python 2.x and 3.x compatible wait for input:
try: input = raw_input
except NameError: pass
# Wait for keystroke (for interactive mode)
input("Press enter when done...")
Run Code Online (Sandbox Code Playgroud)
如果我在这里遗漏了一些明显的东西,我很抱歉,我对 Python 不太熟悉,而且我没有找到关于这个问题的令人满意的信息。但我的直觉告诉我必须有一个简单而优雅的解决方案。
系统信息(我尝试过的最新版本):
谢谢,
弗洛
| 归档时间: |
|
| 查看次数: |
926 次 |
| 最近记录: |