Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Tkinter从内存中删除存储的文件_Python 2.7_Tkinter - Fatal编程技术网

Python 2.7 Tkinter从内存中删除存储的文件

Python 2.7 Tkinter从内存中删除存储的文件,python-2.7,tkinter,Python 2.7,Tkinter,上面的代码创建了一个有三个按钮的程序。加载文件按钮加载一个txt文件,重置按钮用于清除绘图并删除刚刚加载到内存中的文件。“打印”按钮用于将图形打印到下面的画布上 我有一个关于如何编写与重置函数相关联的函数的问题,即clear_file函数。目前它所做的只是从画布上清除绘图。但似乎加载到绘图的文件存储在内存中,因为再次单击绘图,它将显示绘图。我的目标是使用“重置”按钮让它重新开始——内存中没有存储任何内容。我知道加载新文件将覆盖以前的文件。但是,当有多个按钮用于加载不同的文件时,情况可能会变得复杂

上面的代码创建了一个有三个按钮的程序。加载文件按钮加载一个txt文件,重置按钮用于清除绘图并删除刚刚加载到内存中的文件。“打印”按钮用于将图形打印到下面的画布上

我有一个关于如何编写与重置函数相关联的函数的问题,即clear_file函数。目前它所做的只是从画布上清除绘图。但似乎加载到绘图的文件存储在内存中,因为再次单击绘图,它将显示绘图。我的目标是使用“重置”按钮让它重新开始——内存中没有存储任何内容。我知道加载新文件将覆盖以前的文件。但是,当有多个按钮用于加载不同的文件时,情况可能会变得复杂。因此,我希望Reset能够完成这项工作。 如果你想试试这个小程序,你可以创建一个简单的txt文件,里面有两列数据要加载到程序中


谢谢。

我想做两个改变。将设置绘图的代码移动到其自己的方法中,并在每次调用该方法时重置帧。这可以通过销毁框架并重新制作来实现。第二个更改我将在重置时更改命令以引用此新方法

拿着你的代码,这是我将要改变的,以便能够重置绘图

    from Tkinter import *
    import Tkinter as tk
    import ttk
    import tkFileDialog
    import numpy as np
    import matplotlib
    matplotlib.use("TkAgg")
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    from matplotlib.figure import Figure

    class Look():
        def __init__(self, master):
            self.master = master
            self.master.title("Demo")
            self.master.configure(background = "grey91") #the color will be changed later
            self.master.minsize(500, 300) # width + height
            self.master.resizable(False, False)

            self.top_frame = ttk.Frame(self.master, padding = (10, 10))
            self.top_frame.pack()

            ttk.Button(self.top_frame, text = "Load file", command = self.load_file,
               style = "TButton").pack()

            ttk.Button(self.top_frame, text = "Reset", command = self.clear_file,
                   style = "TButton").pack()

            ttk.Button(self.top_frame, text = "Plot", command = self.plot_file,
                              style = "TButton").pack()

            self.bottom_frame = ttk.Frame(self.master, padding = (10, 10))
            self.bottom_frame.pack()

            self.fig = plt.figure(figsize=(12, 5), dpi=100) ##create a figure; modify the size here
            self.fig.add_subplot()

            plt.title("blah")

            self.canvas = FigureCanvasTkAgg(self.fig, master = self.bottom_frame)
            self.canvas.show()
            self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

            self.toolbar = NavigationToolbar2TkAgg(self.canvas, self.bottom_frame)
            self.toolbar.update()
            self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)

        def load_file(self):
            self.file =  tkFileDialog.askopenfilename(defaultextension = ".txt", filetypes = [("Text Documents", "*.txt")])

        def clear_file(self):
            self.fig.clf()
            self.fig.add_subplot()

            plt.xticks()
            plt.yticks()

            self.canvas.draw()


        def plot_file(self):
            self.r, self.g = np.loadtxt(self.file).transpose()
            self.fig.clf()
            plt.plot(self.r, self.g)
            self.canvas.show()


    def main():
        root = Tk()
        GUI = Look(root)
        root.mainloop()

    if __name__ == "__main__": main()

不要导入tkinter两次。从Tkinter import*中删除行
,只需使用
import Tkinter as tk
。没有理由导入tkinter两次。也就是说,您可以将绘图存储为类属性,然后使用重置按钮销毁该属性。另外,如果你把所有的东西都放在一个框架中,然后破坏这个框架,那么它就会破坏这个框架中的所有内容。然后你可以简单地重拍一切。谢谢。这听起来很有用。您能为这个玩具示例提供一段代码来实现它吗?
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
import numpy as np
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure

class Look():
    def __init__(self, master):
        self.master = master
        self.master.title("Demo")
        self.master.configure(background = "grey91") #the color will be changed later
        self.master.minsize(500, 300) # width + height
        self.master.resizable(False, False)

        self.top_frame = ttk.Frame(self.master, padding = (10, 10))
        self.top_frame.pack()

        ttk.Button(self.top_frame, text = "Load file", command = self.load_file,style = "TButton").pack()
        ttk.Button(self.top_frame, text = "Reset", command = self.new_plot,style = "TButton").pack()
        ttk.Button(self.top_frame, text = "Plot", command = self.plot_file,style = "TButton").pack()

        self.bottom_frame = ttk.Frame(self.master, padding = (10, 10))
        self.bottom_frame.pack()

        self.new_plot()

    # this function will reset your plot with a fresh one.
    def new_plot(self):
        self.bottom_frame.destroy()
        self.bottom_frame = ttk.Frame(self.master, padding = (10, 10))
        self.bottom_frame.pack()
        self.fig = plt.figure(figsize=(12, 5), dpi=100) ##create a figure; modify the size here
        self.fig.add_subplot()
        plt.title("blah")
        self.canvas = FigureCanvasTkAgg(self.fig, master = self.bottom_frame)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
        self.toolbar = NavigationToolbar2TkAgg(self.canvas, self.bottom_frame)
        self.toolbar.update()
        self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)

    def load_file(self):
        self.file =  filedialog.askopenfilename(defaultextension = ".txt", filetypes = [("Text Documents", "*.txt")])

    def clear_file(self):
        self.fig.clf()
        self.fig.add_subplot()

        plt.xticks()
        plt.yticks()

        self.canvas.draw()


    def plot_file(self):
        self.r, self.g = np.loadtxt(self.file).transpose()
        self.fig.clf()
        plt.plot(self.r, self.g)
        self.canvas.show()

if __name__ == "__main__":
    root = tk.Tk()
    GUI = Look(root)
    root.mainloop()