相关疑难解决方法(0)

pyplot.show() 重新打开旧的 tkinter 对话框

编辑:这似乎是 Mac OS 系统上仅限于 Tcl/Tk 的问题。因此,如果您没有这方面的经验,这个话题可能没有意义......

我想要一个可以做两件事的 Python 脚本:

  1. 通过 Tkinter 文件对话框询问用户文件名。
  2. 绘制所述文件中的一些数据。

问题是,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 …
Run Code Online (Sandbox Code Playgroud)

python macos filedialog tkinter matplotlib

5
推荐指数
0
解决办法
926
查看次数

标签 统计

filedialog ×1

macos ×1

matplotlib ×1

python ×1

tkinter ×1