【问题标题】:"Do not localize" warning after choosing file in Tkinter file dialog在 Tkinter 文件对话框中选择文件后出现“不本地化”警告
【发布时间】:2019-03-26 21:11:43
【问题描述】:

我想要实现的是自动从文件中绘制数据,以下是我的想法:

  1. 使用 Tkinter 创建一个简单的 GUI,放置一些按钮来激活功能。
  2. 定义 read_file() 以使用 tkinter 文件对话框选择文件,并存储数据。
  3. 使用 matplotlib 绘图。

这是我的代码的简单版本:

import matplotlib.pyplot as plt
import tkinter as tk

def read_files():
    import tkinter.filedialog as tkf
    filePath = tkf.askopenfilenames()
    with open(filePath, 'r') as file:
        content = file.read()
    # after some lines of code, get data from content
    # data[0] and data[1] are x and y, respectively
    return data

def plot_data():
    data = read_files()
    plt.figure()
    plt.plot(data[0], data[1])
    plt.show()

#simple GUI
root.tk()
btn = tk.Button(root, ...(some args), command=plot_data)
btn.pack()
root.mainloop()

我的代码工作正常,它可以读取文件和绘图数据,但问题是:每次选择文件后单击“打开”时,文件对话框都没有关闭,并且一个奇怪的窗口显示“不要本地化” " 提示我的数据图,如图所示。

需要注意的是,如果我注释掉 plt.show() 并且只打印数据,这个警告就会消失。

def plot_data():
    data = read_files()
    plt.figure()
    plt.plot(data[0], data[1])
    #plt.show()
    print(data)

我希望我说清楚,我怎样才能摆脱这个烦人的窗口?

【问题讨论】:

    标签: python python-3.x matplotlib tkinter


    【解决方案1】:

    问题解决了,只需在import matplotlib下方和import matplotlib.xxx上方添加一行代码:

    import matplotlib
    matplotlib.use("TkAgg")    # Add in here
    import matplotlib.pyplot as plt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      相关资源
      最近更新 更多