簡體   English   中英

在Tkinter更新畫布

[英]Updating canvas in Tkinter

有人知道如何修改以下代碼,以便每次更改測角函數時都可以更新圖嗎? 繪制是在函數plot() 在stackoverflow上有一些相關的線程,但是我無法將其應用於示例代碼...

很多,

麥基

import numpy as np
import Tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

class App:

    def __init__(self, window):

        self.window = window

        # set up pi
        pi = 3.141592654

        # create the frame and pack to the object
        frame = tk.Frame(window)
        frame.pack()

        def callback(func, *pargs):
            print(func.get())
            func.set(func.get())
            self.plot()
            self.window.update()

        self.func = tk.StringVar()
        self.func.set("sin") # default value
        self.func.trace('w', lambda *pargs: callback(self.func, *pargs))

        self.button = tk.OptionMenu(window, self.func, "sin", "cos", "tan")
        self.button.pack(side = tk.TOP)

        self.min_value = tk.Entry(window, justify = tk.RIGHT)
        self.min_value.pack()

        self.min_value.delete(0, tk.END)
        self.min_value.insert(0, -pi)

        self.max_value = tk.Entry(window, justify = tk.RIGHT)
        self.max_value.pack()

        self.max_value.delete(0, tk.END)
        self.max_value.insert(0, pi)

        self.button = tk.Button(frame, text = "QUIT", foreground = "red", command = frame.quit)
        self.button.pack(side = tk.BOTTOM)

        self.draw = tk.Button(frame, text = "DRAW", command = self.plot())
        self.draw.pack(side = tk.LEFT)

    def plot(self):

        # generate numbers for the plot
        x = np.array(np.arange(np.float64(self.min_value.get()), np.float64(self.max_value.get()), 0.001))

        if self.func.get() == 'sin':
            print('plotting sin()')
            y = np.sin(x)
        elif self.func.get() == 'cos':
            print('plotting cos()')
            y = np.cos(x)
        else:
            print('plotting tan()')
            y = np.tan(x)

        # create the plot
        fig = Figure(figsize = (6, 6))
        a = fig.add_subplot(1,1,1)
        a.plot(x, y, color = 'blue')
        a.set_title ("Goniometric Functions", fontsize = 12)
        a.set_ylabel(self.func.get() + '(x)', fontsize = 8)
        a.set_xlabel('x', fontsize = 8)

        # canvas
        canvas = FigureCanvasTkAgg(fig, master = self.window)
        self.widget = canvas.get_tk_widget().pack()
        print('here I should update canvas')
        canvas.draw()

root = tk.Tk()
app = App(root)   
root.mainloop()
root.destroy()

您應該在畫布上創建空圖,然后僅使用a.set_xdata()a.set_ydata()替換圖中的數據

import numpy as np
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

class App:

    def __init__(self, window):

        self.window = window

        # set up pi
        pi = 3.141592654

        # create the frame and pack to the object
        frame = tk.Frame(window)
        frame.pack()

        def callback(func, *pargs):
            print(func.get())
            #func.set(func.get()) # <-- it has no sense
            self.update_plot()
            self.window.update()

        self.func = tk.StringVar()
        self.func.set("sin") # default value
        self.func.trace('w', lambda *pargs: callback(self.func, *pargs))

        self.button = tk.OptionMenu(window, self.func, "sin", "cos", "tan")
        self.button.pack(side = tk.TOP)

        self.min_value = tk.Entry(window, justify = tk.RIGHT)
        self.min_value.pack()

        self.min_value.delete(0, tk.END)
        self.min_value.insert(0, -pi)

        self.max_value = tk.Entry(window, justify=tk.RIGHT)
        self.max_value.pack()

        self.max_value.delete(0, tk.END)
        self.max_value.insert(0, pi)

        self.button = tk.Button(frame, text="QUIT", foreground="red", command=root.destroy) #<--
        self.button.pack(side = tk.BOTTOM)

        self.draw = tk.Button(frame, text="DRAW", command=self.update_plot)
        self.draw.pack(side = tk.LEFT)

        # create empty plot
        self.create_plot()
        # update plot with current function at start
        self.update_plot()

    def create_plot(self):

        # create the plot
        self.fig = Figure(figsize = (6, 6))
        self.a = self.fig.add_subplot(1,1,1)

        self.p, _ = self.a.plot([], [], color = 'blue')

        self.a.set_title ("Goniometric Functions", fontsize = 12)

        # canvas
        self.canvas = FigureCanvasTkAgg(self.fig, master = self.window)
        self.widget = self.canvas.get_tk_widget().pack()
        #self.canvas.draw()

    def update_plot(self):
        print('update')

        # generate numbers for the plot
        x = np.array(np.arange(np.float64(self.min_value.get()), np.float64(self.max_value.get()), 0.001))

        if self.func.get() == 'sin':
            print('plotting sin()')
            y = np.sin(x)
        elif self.func.get() == 'cos':
            print('plotting cos()')
            y = np.cos(x)
        else:
            print('plotting tan()')
            y = np.tan(x)

        # replace labels
        self.a.set_ylabel(self.func.get() + '(x)', fontsize = 8)
        self.a.set_xlabel('x', fontsize = 8)

        # replace data
        self.p.set_xdata(x)
        self.p.set_ydata(y)

        # rescale
        self.a.relim()
        self.a.autoscale_view()

        # update screen
        self.canvas.draw()

root = tk.Tk()
app = App(root)   
root.mainloop()
#root.destroy() You don't need it

@furas-非常感謝這個職位。 根據您在帖子發布后10分鍾的評論得出的結果。 盡管僅次於您,但我也發送了我的代碼版本;)。 我認為,我們可以根據需要關閉此線程...

麥基

import numpy as np
import Tkinter as tk
import matplotlib.figure as fg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

class App:

    def __init__(self, window):

        self.window = window

        # set up pi
        pi = 3.141592654

        # create empty canvas
        x = []
        y = []
        fig = fg.Figure(figsize = (4,4))
        fig_subplot = fig.add_subplot(111)
        self.line, = fig_subplot.plot(x, y)
        self.canvas = FigureCanvasTkAgg(fig, master = self.window)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side = tk.TOP, fill = tk.BOTH, expand = 1)

        # create the frame and pack to the object
        frame = tk.Frame(window)
        frame.pack()

        def callback(func, *pargs):
            print(func.get())
            func.set(func.get())
            self.plot()

        self.func = tk.StringVar()
        self.func.set("sin") # default value
        self.func.trace('w', lambda *pargs: callback(self.func, *pargs))

        self.button = tk.OptionMenu(window, self.func, "sin", "cos", "tan")
        self.button.pack(side = tk.TOP)

        self.min_value = tk.Entry(window, justify = tk.RIGHT)
        self.min_value.pack()

        self.min_value.delete(0, tk.END)
        self.min_value.insert(0, -pi)

        self.max_value = tk.Entry(window, justify = tk.RIGHT)
        self.max_value.pack()

        self.max_value.delete(0, tk.END)
        self.max_value.insert(0, pi)

        self.button = tk.Button(frame, text = "QUIT", foreground = "red", command = frame.quit)
        self.button.pack(side = tk.BOTTOM)

    def plot(self):

        # generate numbers for the plot
        x = np.array(np.arange(np.float64(self.min_value.get()), np.float64(self.max_value.get()), 0.001))

        if self.func.get() == 'sin':
            print('plotting sin()')
            y = np.sin(x)
        elif self.func.get() == 'cos':
            print('plotting cos()')
            y = np.cos(x)
        else:
            print('plotting tan()')
            y = np.tan(x)

        # update canvas
        self.line.set_data(x, y)
        ax = self.canvas.figure.axes[0]
        ax.set_xlim(x.min(), x.max())
        ax.set_ylim(y.min(), y.max())        
        self.canvas.draw()

root = tk.Tk()
app = App(root)   
root.mainloop()
root.destroy()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM