在Tkinter文件对话框中选择文件后出现“请勿本地化”警告

2024-04-26 21:32:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  1. 使用Tkinter创建一个简单的GUI,放置一些按钮来激活函数。你知道吗
  2. define read\ u file()使用“文件”对话框选择文件,并存储数据。你知道吗
  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()

我的代码工作得很好,它可以读取文件和打印数据,但问题是:每次我在选择文件后单击“打开”,文件对话框都没有关闭,一个奇怪的窗口显示“不要本地化”提示我的数据打印,如图所示。你知道吗

enter image description here

应该注意的是,如果我发表评论节目()并且仅打印数据时,此警告将消失。你知道吗

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

我希望我说清楚,我怎样才能摆脱这个讨厌的窗口?你知道吗


Tags: 文件数据importreaddataplotdefas